Baakey

May 14, 2021

Regex 101

Playground

=> Start with word

  • ^word

=> End with word

  • word$

=> Match any of

  • gr[io]s
  • gris|gros

=> Match any letters/numbers

  • [a-zA-Z0-9]

=> Refuse any number

  • [^0-9]

=> Specify an optional letter/word 0 or 1

  • a?

=> Specify an optional letter/word 0 or ++

  • a*

=> Specify a mandatory letter/word

  • a+
  • [0-9]+

=> Specify range | ? === {0,1} | + === {1,} | * === {0,}

  •  a{3,5}
  •  a{3,}

=> Quantifiers

  • *: 0 or more
  • +: 1 or more
  • ?: 0 or 1
  • {3: Exactly 3
  • {3,}: 3 or more
  • {3,5}: 3, 4 or 5

=> Pattern Modifiers

  • g: Global match
  • i: Case-i­nse­nsitive
  • m: Multi-line mode. Causes ^ and $ to also match the start/end of lines.
  • s: Single-line mode. Causes . to match all, including line breaks.
  • x: Allow comments and whitespace in pattern
  • e: Evaluate replac­ement
  • U: Ungreedy mode

=> Shortcuts

  • \d: [0-9]
  • \D: [^0-9]
  • \w: [a-zA-Z0-9_]
  • \W: [^a-zA-Z0-9_]
  • \t: tab
  • \n: newline
  • \r: backspace
  • \s: whitespace
  • \S: nor \n \s \t
  • .: everything
  • frenchTelephoneNumber: ^0[1-68]([-. ]?[0-9]{2}){4}$
  • email: ^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$