Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • <pattern> (a regular expression, or regex) can contain special match metacharacters and modifiers. The ones below are Perl metacharacters, which are supported by most languages (e.g. grep -P)
    • ^ – matches beginning of line
    • $ – matches end of line
    • .  – (period) matches any single character
    • * – modifier; place after an expression to match 0 or more occurrences
    • + – modifier, place after an expression to match 1 or more occurrences
    • ? – modifier, place after an expression to match 0 or 1 occurrences
    • \s – matches any whitespace character (\S any non-whitespace)
    • \d – matches digits 0-9
    • \w – matches any word character: A-Z, a-z, 0-9 and _ (underscore)
    • \t matches Tab
    • \n matches Linefeed\r matches Carriage return
    • [xyz123] – matches any single character (including special characters) among those listed between the brackets [ ]
      • this is called a character class.
      • use [^xyz123] to match any single character not listed in the class
    • (Xyz|Abc) – matches either Xyz or Abc or any text or expressions inside parentheses separated by | characters
      • note that parentheses ( ) may also be used to capture matched sub-expressions for later use
  • in Perl, where a pattern is delimited by //, modifiers appear after the pattern:
    • i - perform case-insensitive text matching
    • g - perform the specified substitution globally on each input record, not just on the 1st match

...