Regular expressions (regex) are powerful tools for pattern matching and searching in strings. They are widely used in programming for tasks like data validation, parsing, and text processing.
Here are some common regex patterns you might find useful:
Common Characters
- Alphanumeric Characters:
[a-zA-Z0-9]
- Digits:
\d
- Letters:
[a-zA-Z]
Special Characters
- Any Character:
.
(matches any character except newline) - Specific Character:
[char]
- Character Range:
[a-z]
(matches any character between a and z) - Word Boundary:
\b
(matches the position between a word and a non-word character)
Quantifiers
- Zero or More:
*
(matches 0 or more of the preceding element) - One or More:
+
(matches 1 or more of the preceding element) - Exactly N Times:
{n}
(matches exactly n occurrences of the preceding element) - At Least N Times but Not More:
{n,}
(matches at least n occurrences of the preceding element) - Between N and M Times:
{n,m}
(matches between n and m occurrences of the preceding element)
Examples
- Email Address:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
- Phone Number:
\+?[0-9]{1,3}?[-. ]?[(]?[0-9]{3}[)]?[-. ]?[0-9]{3}[-. ]?[0-9]{4}
- URL:
http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+
Resources
For more information and advanced regex patterns, check out our Regex Reference.
Regex Pattern