Regular expression syntax knowledge

metacharacter classification

    1. Character matching

        . means any single character that matches

    2. Number of matches

        * The preceding character appears any number of times, including 0 times

        \? The preceding character appears 0 or 1 time

         The character preceding \+ appears one or more times

         The character preceding \{n,m\} appears at least n times and at most m times 

    3. Position anchoring

        The default search method of regular expressions is to contain, without limiting the position where the specified string appears.

        start of line ^

        end of line $

        word prefix \<

        word ending \>

 

     4. Grouping

         \(    \)

       

 

  Example of Regular Expression Combination Usage

    1. How to remove the percent sign or other special symbols after the number? Grep the filtered result again to achieve the corresponding effect, which is equivalent to multiple grep

      

      

 

    2.cat       /etc/fstab   |     grep -v "^#"  |    grep -v     "^[[:space:]]*$"

      [[:space:]]* When the number of matches appears in the regular expression, it and the preceding character expression should be treated as a whole

      [[:space:]] means that a space must appear on a line

      [[:space:]]* means that there can be no spaces or 1 or any number of spaces in a line, it does not mean that a space must appear first and then it can be judged that a space can appear or not appear after the space

 

    3. Group matching results

      \1 represents the result string matched by the previous grouping regular expression, not the regular expression pattern itself. The string matched by the previous (r..t) is root, so \1 represents root instead of r..t

     

     

     

      

 

      regular expression or syntax

           1. echo axy | grep "a\|bxy" means that you need to match the character a or the string bxy instead of matching axy or bxy

           2. echo axy | grep "(a\|b)xy" means match axy or match bxy

 

 

     

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644556&siteId=291194637