- Unbelievably Powerful
- highly reusable and highly portable
- Solve countless day to day problems
- Widely Supported
A pattern matching language. Yes, a language!
A regex is a string. For instance, foo
is a regex. so is [A-Z+:\d+]
There's a beautiful language behind that obfuscated mess of characters.
Let us start with basic patterns without weird characters.
abc
a thing, followed by another thing, followed another thing!
pattern | matches |
---|---|
abc |
abc,abcd,zabc |
123 | 1234,abc123,za234123bc |
a1 |
a1,a1bc,a12b2c |
Characters with special meaning to them.
^ [] () {} . * \ | + ? $
and sometimes -
Matches any character. (Wild card)
s.n
matches san
, sun
, son
and etc.
^
- Start of Line; $
- End of Line
pattern | matches |
---|---|
^Hi |
line starts with Hi |
Hello$ |
line ends with Hello |
^san$ |
line that has only san |
^$ |
Empty line |
match any one of several characters.
gr[ea]y - "g
, followed by r
, followed by either an e
or an a
, followed by y."
If you use [^...]
instead of [...]
, the class matches any character that's not in the character class.
specify range between two characters
pattern | matches |
---|---|
[a-c] |
Any of a b c |
[0-9] |
Any of 0 1 2 3 4 5 6 7 8 9 |
[A-Za-z] |
Any of the English alphabet |
[012345abcedfABCDEF]
is same as [0-5a-fA-F]
|
means either
- combines two expressions into one
Mike
and Michael
are separate, but Mike|Michael
is one that matches either.
placed after a character that is allowed, but not required, at a certain position in an expression.
flavou?r
- matches flavor (US), flavour (UK)
"f
, followed by l
, followed by a
, followed by v
, followed by o
, followed by an optional u
, followed by r
."
number of times the preceding character can appear in an expression
Character | Meaning |
---|---|
? |
0 or 1 |
+ |
1 or more |
* |
0 or more |
{min,max}
metasequence specifies number of times an item can match
TODO: add example
\
escapes metacharacters so you can match those in patterns.
TODO: add example
http://([^/]+)
- matches the domain part alone.
http://github.com/sanspace
github.com
[^/]+
is grouped as subportion of the expression
Requirements
- Alphanumeric
- No special characters except underscore
- Minimum 8 characters, Maximum 16 characters
^[a-zA-Z0-9_]{3,16}$
169.254.169.254
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
not that regular expressions are evil, per se, but that overuse of regular expressions is evil.
- Know the flavors of different programming languages
- Know what you are doing
- RegEx could be slow for complex patterns