Programming‎ > ‎Language‎ > ‎

Regex

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Regular expressions is used to search in text.

Standard character classes
  • A character class is created by putting things inside a []
  • So gr[ae]y match both gray and grey
  • Use a - inside to specify a range. [0-9] for a single digit between 0 and 9.
  • It's possible to specify more then one range and single characters. Ex [0-9a-fq] is 0-9, a-f or q.
  • Use a caret after the opening square to negate the class. [^0-9] will match any non number character.

Standard Character set
  • Some character sets is so common that shorthand names already exist for them
  • \d match a single character digit
  • \w Match alphanumeric characters and _.
  • \s Match white space characters.
Quantifiers
 * 0 or more
 +  1 or more
 ? 0 or 1
 {4} Exactly 4
 {4,} 
 4 or more
 {4,6} 
 4, 5 or 6

Examples
%x (\w+)
Search for %x and capture the word behind it.

%xx (.*?) %xx
Capture the text between %xx and %xx. To use the capture text in a search and replace use \1 where the number is the index of the capture group. So use <b>\1</b> as the replace text to replace %xx Hello world %xx with <b>Hello World</b>.

^@title\s+(.*)
At start of line (^) find @title, skip any number of spaces behind it (\s+) and then capture the rest of the text (.*).

^(#+)\s+(.*)
At start of line capture any number of # (#+). Skip space and then capture the rest of the text.

Comments