1/1/1970
The logic of writing a regex (regular expression) formula lies in understanding patterns in the data you want to match. Here’s a step-by-step guide:
Analyze the format of the data you want to match:
123-456-7890)?John, John Doe)?/, -, .)?Identify individual parts of the pattern:
Use special characters and quantifiers to describe the data.
| Character/Pattern | Description | Example |
|---|---|---|
. |
Matches any single character | a.b matches aab, acb |
\d |
Matches a digit (0-9) |
\d{3} matches 123 |
\w |
Matches a word character (a-z, A-Z, 0-9, _) |
\w+ matches hello123 |
\s |
Matches whitespace | \s matches a space or tab |
[] |
Matches any character in the set | [aeiou] matches any vowel |
^ |
Matches the start of the string | ^Hello matches Hello world |
$ |
Matches the end of the string | world$ matches Hello world |
* |
Matches 0 or more repetitions | a* matches aaa or empty |
+ |
Matches 1 or more repetitions | a+ matches aaa but not empty |
? |
Matches 0 or 1 repetition | a? matches a or empty |
{n} |
Matches exactly n repetitions |
a{3} matches aaa |
{n,m} |
Matches between n and m repetitions |
a{1,3} matches a, aa, aaa |
| ` | ` | Logical OR |
() |
Groups patterns | (abc)+ matches abcabc |
\ |
Escapes a special character | \. matches . literally |
123-456-7890: \d{3}-\d{3}-\d{4}user@example.com:\w) or . before @..com, .org, etc.\b\w+(\.\w+)*@\w+\.\w+\bDD/MM/YYYY or YYYY-MM-DD:/ or -), two digits, separator, four digits.(\d{2}[-/]\d{2}[-/]\d{4})
or
(\d{4}[-/]\d{2}[-/]\d{2})Test Your Regex:
Use online tools like Regex101 or Pythex to test your regex.
In Python, you can test it like this:
import re
pattern = r'\d{3}-\d{3}-\d{4}'
text = "Call me at 123-456-7890 or 987-654-3210."
matches = re.findall(pattern, text)
print(matches) # Output: ['123-456-7890', '987-654-3210']Key tips:
\ to escape special characters like ., ?, *, etc.By analyzing the structure of the data and applying these components, you can write any regex formula!