You can use the Expresso tool for build and test regular expressions.
Special characters:
. Match any character except newline \w Match any alphanumeric character \s Match any whitespace character \d Match any digit \b Match the beginning or end of a word ^ Match the beginning of the string $ Match the end of the string
Repetitions:
* Repeat any number of times
+ Repeat one or more times
? Repeat zero or one time
{n} Repeat n times
{n,m} Repeat at least n, but no more than m times
{n,} Repeat at least n times
Ranges samples:
[aeiou] matches any vowel [.?!] matches the punctuation [a-z0-9] match any lowercase letter of the alphabet, or any digit
Negations:
\W Match any character that is NOT alphanumeric \S Match any character that is NOT whitespace \D Match any character that is NOT a digit \B Match a position that is NOT the beginning or end of a word [^x] Match any character that is NOT x [^aeiou] Match any character that is NOT one of the characters aeiou
Some samples (there are a lot of best and optimal samples on Internet, look for them or use Expresso to build them):
\b\d\d\d-\d\d\d\d Find seven-digit phone number
\b\d{3}-\d{4} Find seven-digit phone number a better way
^\d{3}-\d{4}$ Validate a seven-digit phone number
\d{3}-\d{2}-\d{4} Social security number
\ba\w*\b Find words that start with the letter a
\b\w{6}\b Find six letter words
\b\w{5,6}\b Find all five and six letter words
(\d{1,3}\.){3}\d{1,3} A simple IP address finder
For more information about grouping, positive and negative lookarounds, Greedy and Lazy, please please take a look to this great article by Jim Hollenhorst.
0 comments:
Post a Comment