Regex

Regular expressions are used to search and manipulate text based on patterns. Use regex101 or regexr online tools for validating constructed regex and obtaining detailed descriptions of the regex for further debugging.

Usage

  • ^ (caret) - Specify start of the line
  • $ - Specify end of the line
  • ^$ - regex for empty lines
  • . (dot/period)- single character match except the end of line character match
  • * - zero or more occurrence of the previous character
  • + or \+ - one or more occurrence of the previous character
  • ? or \? - zero or one occurrence of the previous character
  • [0-9] - Character class list of characters which can be included
  • [^aeiou] - exception in the character class, i.e NOT include the specified list of characters in the regex
  • | - OR operation for matching multiple sub-expression
  • [:digit:] or \d - Character class Only the digits 0 to 9
  • [:alnum:] - Character class Any alphanumeric character 0 to 9 OR A to Z or a to z.
  • [:alpha:] or \D (not digit)- Character class Any alpha character A to Z or a to z.
  • [:blank:] - Character class Space and TAB characters only.
  • \w - match Word
  • \s - match whitespace characters
  • {m,n} - preceding item is matched at least m times, but not more than n times
  • {m} - Exact M occurrence
  • {m,} - M or more occurrences
  • \b - Word boundry

Examples

  • Regex for matching the IP
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

# If it starts with 25, next number should be 0 to 5 (250 to 255)
# If it starts with 2, next number could be 0-4 followed by 0-9 (200 to 249)
# zero occurrence of 0 or 1, 0-9, then zero occurrence of any number between 0-9 (0 to 199)
# Then dot character
  • Valid Email
^[a-zA-Z0–9+_.-]+@[a-zA-Z0–9.-]+$