CSC128: Introduction to Linux

Regular Expressions


Usage

Regular expressions are used with programs such as vi, grep, awk and sed. See pages 887-893 (we will not be covering the Extended Set of REGEX) for a full treatment of regular expressions. A character is any character except NEWLINE.

    Classes of Characters in Regular expressions
    $ ; | * ? ' " ` [ ] ( ) $ < > ^ { } # / \ % ! ~
  • Quoting Special Characters \
  • Delimiters, usually a / - mark start and end of regular expression
  • Special Characters - default to finding the longest string that is a match
    • a space is a regular char in a REGEX and does not need to be quoted
    • . dot - matches any single character
    • square brackets [ ]- define a character class
    • caret is special in 2 ways
      1. match the beginning of a line
      2. caret following a square bracket [^) means NOT cont ained within the list of character class. [^a-zA-Z] means any character that is NOT a letter. Note how a hyphen - indicate s a range in character class.
    • Inside of a character class, \ and * are regular characters, not special characters.
    • * asterisk, zero or more occurrences of a match of the regular expression that comes before it. .* matches any str ing.
    • $ end of a line.
    • \> = end of word, and \< = beginning of word.
  • | means or separator. example /Q|M/ finds a Q or an M.