-
Notifications
You must be signed in to change notification settings - Fork 280
Regular Expressions
A regular expression (or "regex") is a sequence of characters that is used to match a longer list of characters.
For example, this is a regular expression:
[df]og
which matches the words dog and fog.
You can use different combination of symbols in order to match different sets of characters.
"match" means "there is/are one or more matches in the text".
For example, let's take this text:
"There's a dog running in the fog."
regex 1:
dog
matches: 1
This regex matches the text.
regex 2
[df]og
matches: 2
This regex matches the text.
regex 3
rain
matches: 0
This regex doesn't match the text.
Square brackets are used whenever you want to establish an "OR" relation between what's inside them.
For example
[ab]
matches "a" and "b".
This regex:
file[01234]
will match
- file0
- file1
- file2
- file3
- file4
A quantifier is a symbol that tells how much times you want to match a specific character.
| Symbol | Times | Example | Example match |
|---|---|---|---|
| ? | 0-1 times | hello? |
"hell" or "hello" |
| + | >= 1 times | hello+ |
"hello" or "helloo" or "hellooo" or ... |
| * | >= 0 times | hello* |
"hell" or "hello" or "helloo" or ... |
| {n} | exactly n times | hel{2}o |
"hello" |
| {n,m} | n to m times | hel{0,3}o |
"heo" or "helo" or "hello" or "helllo" |
Dot means any character!
For example, this regex:
.{4}
will match any word long exactly 4 characters.
- Rubular -> a regex tester
- Regex Primer: Part 1 -> this is the tutorial which I followed when I was learning regular expressions
- Regex Primer: Part 2 -> as above, but the second part
Francesco Andreuzzi, Italy, andreuzzi.francesco@gmail.com