Skip to content

Regular Expressions

Francesco edited this page Jul 8, 2017 · 8 revisions

What is a regular expression

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.

What do you mean for "match"?

"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 []

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

Quantifiers

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 (.)

Dot means any character!

For example, this regex:
.{4} will match any word long exactly 4 characters.

Great sites

Clone this wiki locally