Linux Shell Regular Expression usage

Source: Internet
Author: User

  

1, "\" usage

Used to turn off the special meaning of its subsequent characters, to restore the character's own meaning, such as: \ \ represents the character \

2, "." Usage

Match any single character

3, "*" Usage

Matches any character, can be a single, or multiple, and the "." Character of the go Bar is it possible to match more than one arbitrary character

4. Usage of "^"

Starts at the beginning of the line to match the immediately following characters, such as ^6, that match the first character of the row to a line of 6.

5. Usage of "$"

Similar to the "^" usage, which represents the line at the end of a line that matches a character, such as 6$, that matches the end of a row with a character of 6.

6. Usage of "[]"

Matches a range of characters characters bracket expressions, matching any character within it. Where-the range of consecutive characters is represented, and the first character in square brackets has a reverse meaning, which matches any character that is not within the list (square brackets). If you want the] and-to indicate its original intent, you need to place it at the first character position of the square brackets, such as []ab] or [-ab], if both characters exist at the same time, it will be] placed at the first character position--placed at the tail end, such as []ab-].

such as [a-ba-z0-9!] Represents all uppercase and lowercase letters, numbers, and exclamation marks. [^ABC] represents all characters except A, B, and C. [Tt]om, can match Tom and Tom.] [-] matches all characters stored in the character's or-.

7, "\{n,m\}" usage

Interval expression, which matches the interval of the number of occurrences of a single character before it, such as \{n\} means repeated n times, and \{n,\} means at least repeated n times; \{n,m\} means repeating n to M times.

such as: grep ' ab\{2,4\} ' * Can filter out ABB, ABBB and abbbb; grep ' ab\{2\} ' * Can filter out ABB, ABBB and ABBBB

8, "\ (... \)" Usage

The pattern between parentheses is stored in a special "reserved space". You can store up to 9 independent sub-patterns in a single mode. Text that matches the sub-pattern can be reused in the same pattern by \1 the escape sequence to \9.

such as: \ (ab\). *\1 indicates that the AB combination appears two times, there can be any number of any characters between two times, such as Abcdab, ABAB, etc., where \1 means match ab

9. Usage of "\b"

Represents the beginning or end of a word, that is, the dividing line of a word, such as grep ' he ' may be leh hehe, but grep ' \bhe\b ' will only appear as a single character "he"

Turn:

If you're looking for a hi, not far behind, follow a Lucy, you should use \bhi\b.*\blucy\b.

Here,. is another meta-character that matches any character except the line break. * is also a meta-character, but it does not represent a character, nor a position, but a quantity-it specifies that the contents of the front can be reused any number of times to match the entire expression. Therefore,. * Together means any number of characters that do not contain a newline. Now the meaning of \bhi\b.*\blucy\b is obvious: first a word hi, then any arbitrary character (but not a newline), and finally the word Lucy.

10. Usage of "\d"

Used to match any one digit

0\d\d-\d\d\d\d\d\d\d\d matches such a string: starting with 0, then two digits, then a hyphen "-", and finally 8 digits (that is, China's phone number.) Of course, this example only matches the case where the area code is 3 bits).

The \d here is a new meta-character that matches one number (0, or 1, or 2, or ...). -not a meta-character, only matches itself-a hyphen (or a minus sign, or a middle line, or whatever you call it).

To avoid so many annoying repetitions, we can also write this expression:0\d{2}-\d{8}. Here {2} ({8}) behind \d means that the front \d must match 2 times consecutively (8 times).

11. Usage of "\s"

Matches any whitespace character, including spaces, tabs (tab), line breaks, Chinese full-width spaces

12. Usage of "\w"

Match letters or numbers or underscores or kanji

The following transfer (regular expression 30-minute introductory tutorial: http://deerchao.net/tutorials/regex/regex.htm)

\ba\w*\b matches a word that begins with the letter A-first at the beginning of a word (\b), then the letter A, then any number of letters or numbers (\w*), and finally the end of the word (\b).

OK, now let's talk about what the word in the regular expression means: it's not less than a continuous \w. Yes, it does not really matter with the thousands of things that you have to memorize when learning English:)

\d+ matches 1 or more consecutive digits. Here the + is and * similar to the meta-character, the difference is * Match repeat any time (may be 0 times), and + match repeat 1 or more times.

\b\w{6}\b matches exactly 6 characters of a word.

^\d{5,12}$ { 5,12} here is similar to the {2} described earlier, except that {2} matches can only be repeated 2 times more,{5,12} is The number of repetitions cannot be less than 5 times, not more than 12 times, or they do not match.

Because ^ and $ are used, the entire string entered is used to match the \d{5,12}, which means that the entire input must be 5 to 12 digits, so if the QQ number entered matches the regular expression, then it will meet the requirements.

Similar to ignoring case options, some regular expression processing tools have an option to handle multiple rows. If this option is selected, the meaning of ^ and $ becomes the beginning and end of the matching line.

13. Antisense Code

Code/Syntax Description
\w Match any characters that are not letters, numbers, underscores, kanji
\s Match any character that is not a whitespace character
\d Match any non-numeric character
\b Match a position that is not the beginning or end of a word
[^x] Matches any character except X
[^aeiou] Matches any character except for the letters AEIOU

When a regular expression contains a qualifier that can accept duplicates, the usual behavior is to match as many characters as possible (in order for the entire expression to be matched). Take this expression as an example:a.*b, which will match the longest string starting with a and ending with B. If you use it to search for Aabab, it will match the entire string Aabab. This is called a greedy match.

Sometimes we need more lazy matching, which is to match as few characters as possible . The qualifier given above can be converted to lazy matching mode, just add a question mark after it . So . * is meant to match any number of repetitions, but with minimal repetition in the premise that the entire match succeeds. Now look at the lazy version of the example:

A.*?b matches the shortest, starting with a string ending with B. If you apply it to Aabab, it will match AaB (first to third character) and AB (fourth to fifth characters).

table 5. Lazy qualifier
code/syntax description
*? Repeat any number of times, but with as few repetitions as possible
+? Repeat 1 or more times, but with as few repetitions as possible
repeat 0 or 1 times, but repeat as little as possible
{n,m}? Repeat N to M times, but with as few repetitions as possible
{n,}? Repeat more than n times, but with as few repetitions as possible

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.