Regular expressions - character classes, construction of choice, meta-sequences

    J. Friedl’s book, Regular Expressions, has a beautiful tablet that I want to quote here.
    Regular expressions open up wide possibilities for searching and replacing in any text. Using regular expressions, you can flexibly and simply process text documents. One of the simplest applications of regular expressions is to search for text - many text editors provide the ability to search by regular expression patterns . In  regexp there are several types of metacharacters that perform different functions, let's briefly consider them:
    • ^ and  $ - “bind” matches of the rest of the regular expression to the beginning and end of the line, respectively. For example, it ^catmatches the lines “cat” and “caterpillar”, and the  dog$lines “bulldog” and “hotdog”.
    • […] -  character classes , allow you to list the characters that may be in a given position of the text. For example, it gr[ea]ymatches the lines “gray” and “gray”.
    • [^…] -  excluding character classes , allow you to list characters that cannot be in a given position of the text. For example, it g[^ae]rdydoes not match the lines "gardy" and "gerdy", but it matches the lines "gurdy", "g3rdy" and "girdy".
    • (…|…) -  design choice , a choice of several options. It should be noted that each of the parts of the choice construct is a full-fledged regular expression . For example, Jeff(re|er)ymatches the strings Jeffrey and Jeffery.
    • \< and  \> - metasequences are analogous to ^ and $ but at the level of words. For example, matches the single word cat.\
    It should be noted that the rules that determine the composition of supported metacharacters (and their interpretation) within the character and excluding character classes and beyond are completely different.
    Regular Metacharacters
    Single character elements
    .pointany one character
    […]character classany of the listed characters
    [^…]inverted character classany character not listed in the class
    \символshieldingif the prefix "\" precedes the character, the character is interpreted as the corresponding literal
    ?question markone instance allowed
    (none required)
    *starany number of instances allowed
    (none required)
    +a plusone instance required;
    any number of instances allowed
    {min, max}interval quantifier *“min” copies are required, “max” results are allowed
    Positional metacharacters
    ^cover circumflexposition at the beginning of the line
    $dollarposition at the end of the line
    \<word boundary *position at the beginning of a word
    \>word boundary *position at the end of a word
    Other metacharacters
    |design of choiceany of the above expressions
    (…)round bracketsconstraint for choice construct, grouping for applying quantifiers, and saving text for backlinks
    \1, \2, …trackbacktext that previously coincided with the first, second, etc. pairs of parentheses
    * These features are not supported by all versions of egrep

    Also popular now: