Regular Expressions (regex)
Table of Contents
Explanation of Regex Elements:
pattern | description |
---|---|
\d | Matches any digit (0-9). |
{n} | Matches the preceding element exactly n times. |
{n,m} | Matches the preceding element between n and m times. |
? | Makes the preceding element optional (matches 0 or 1 time). |
[ ] | Matches any single character within the brackets. |
\. | Escaped dot, matches a literal dot. |
\( | Escaped opening parenthesis, matches a literal opening parenthesis. |
\) | Escaped closing parenthesis, matches a literal closing parenthesis. |
Search for a regex pattern
rg "regex_pattern" /path/to/folder
Search for phone numbers:
Simple 10-digit number (e.g., 1234567890). This can give too generic results. \d{10}
Matches exactly 10 digits.
rg "\d{10}" /path/to/folder
rg "\d{10}" ./
Number with hyphens (e.g., 123-456-7890). \d{3}-\d{3}-\d{4}
Matches three digits, a hyphen, three digits, another hyphen, and four digits.
rg "\d{3}-\d{3}-\d{4}" /path/to/folder
Number with spaces or dots (e.g., 123 456 7890 or 123.456.7890) [ .-]?
Matches a space, dot, or hyphen, optionally (?)
rg "\d{3}[ .-]?\d{3}[ .-]?\d{4}" ./
rg "\d{3}[ .-]?\d{3}[ .-]?\d{4}" ./ --type org
Number with area code in parentheses (e.g., (123) 456-7890). \(?\d{3}\)?
Matches an optional opening parenthesis, three digits, and an optional closing parenthesis.
rg "\(?\d{3}\)?[ .-]?\d{3}[ .-]?\d{4}" /path/to/folder
More flexible pattern (including international numbers). \+?
Matches an optional plus sign for international dialing. \d{1,3}
Matches one to three digits for the country code.
rg "\+?\d{1,3}[ .-]?\(?\d{3}\)?[ .-]?\d{3}[ .-]?\d{4}" /path/to/folder
Reading material
- https://www.rexegg.com/regex-lookarounds.php
- https://jenkov.com/tutorials/java-regex/index.html
- https://www.honeybadger.io/blog/a-definitive-guide-to-regular-expressions-in-go/
- https://www.honeybadger.io/blog/replacing-regular-expressions-with-parsers/