Regular Expression Basic

1. Basic Regular Expression  

  a. "^"     matching the head of a line. "^" must be the first character in a regular expression ,else it only a common character.

  b. "$"  matching the end of a line. "$" must be the last character in a regular expression ,else it only a common character.

    Attention: The "^" is only an anchor if it is the first character in a regular expression,else it only a common character.

          The "$" is only an anchor if it is the last character in a regular expression,else it also only a common character.

          If you want match a "^" in the begin of a line or "$" in the end of the line ,you can use the special characters with a backslash ().

      eg:   ^$ , It can match a blank line

    

  c. "."  matching any silger word, except the end-of-line character

  d. "*"  Repeating previous character zero or many times.

      Attention:

         You must remember that modifiers like "*" , "{3,5}" ,"{3,}" and"{3}" only act as modifiers if they follow a common charater set.

      eg:

       *   : Any line contains an asterisk

       *  : Any line contains an asterisk

         ^*  : Any line starting with an asterisk

  e. [...] Specifying a range of characters

      If you want to mathc specific characters,you can use the square brackets to identify the exact characters you are searching for. The pattern that will match any line of text that contains exactly one numer is.

      Attention: The characters "]' and "-" do not have a special meaning if thed directly follow "[".

      eg:

      []      The common character "[]"

      [-0-9]    Any number or "-"

      [^0-9]    Any character other than a number

      [0-9-]    Any number or "-"

      []0-9]    Any number or "]"

      [0-9-z]    Any number, or any character between "9" and "z“

      [0-9-a]]   Any number, or "-" or "a" or "]"

      POSIX

      [:alnum:]  Alphanumeric

      [:cntrl:]    Control character

      [:lower:]   Lower case character

      [:space:]   whitespace

      [:alpha:]  Alphabetic

      [:digit:]   Digit

      [:upper:]   Upper case character

      [:blank:]   whitespace, tabs, etc.

2. Extended Regular Expression

     a. Repeating previous character minimum and maximum times.

    {3,5}  : Repeating previous character 3,4 or 5 times.

    {3,}    : Repeating previous character 3 or more times.

    {3}   : Repeating previous character only 3 times.

    eg:

       ^A{2,5}B      : Any line starting with 2,3,4 or 5 "A"s followed by a "B" 

       ^A{3,5}    : Any line starting with 3,4 or 4 "A"

       ^A{3}       : Any line starting with 3 "A"

       {3,5}     : Any line contains {3,5}

  b.  + ,  Repeating the privouse character at least one times

  c. ?  , Repeating the privouse character zero or one times.

  d. |    It is meaning or between two regular expression.

    eg:

      ( expressin1  | expression2  | expression3 )

3 operator priority

    Regular expression operator priority is following:

            

      []

      *, +, ?, {m}, {m,}, {m,n}

      common character

      ^, $

      |  

原文地址:https://www.cnblogs.com/ordili/p/3996158.html