Regular Expression notes

Source: Internet
Author: User
Tags uppercase letter
1. entry 1.1 what is a regular expression 1.2 What can a regular expression do? 1.3 How to Use it 2. match 2.1 plain text match 2.2 Special Character match 3. advanced Application 3.1 back-to-reference 3.2 condition branch 4. example of several Regular Expressions 1. entry 1.1 what is a regular expression in simple terms, a regular expression is a string used to match and process text. It has its own special syntax and instructions, but it is not a complete programming language. It is one of the most powerful tools for processing sub-streams. 1.2 What can regular expressions do? A) match a specific character or string in a text file. B) generate code in a specific format, for example, link in HTML. c) check some formats? Correctness, for example, email format D) the most basic function of replacing a regular expression in a text file is to find and replace it, regular Expressions are often used to solve these two problems. For example, the grep series commands are used to match strings and replace regular expressions in Emacs. 1.3 before using a regular expression, it must be clarified that there is no regular expression program. It is only implemented in some programming languages or development tools. Therefore, before using a regular expression, make sure that your tool or language has an engine for interpreting regular expressions. Note that the engines of Regular Expressions in different languages and tools are different. When using regular expressions, there are usually many solutions to a problem, with no specific right or error. 2. matching 2.1 plain text matching there are many regular expression testing tools in Linux, such as using grep directly and embedding Regular Expressions in shell. emacs is the best battlefield for using regular expressions, there are also specialized regular expression learning tools, such as Kiki. Here we will take Kiki as an example to learn. Text Hello, I am a boy, I love csnow, I am learning Linux kernelmy homepage is http://elvalad.wordpress.com Regular Expression Linux results now, I am learning (Linux) kernel this is the simplest Regular Expression matching, the regular expression function is far from the same. 2.2 special characters matching the previous static plain text cannot reflect the strong regular expression at all. The following examples are more practical. Some characters in a regular expression have special meanings. These special characters are generally called metacharacters. Below are some common metacharacters:
------------------------------------------------------ Metacharacter | meaning -------------------------------------------- [] | Character Set combination, match at least one character in the set. character range [^] | evaluate Character Set combination non------------------------------------------------- \ | escape character range \ B | return character (match word boundary) ------------------------------------------------------ \ B | does not match the word boundary -- Break \ f | break \ n | break \ r | carriage return character \ t | Tab character \ v | vertical Tab character \ d | any single number ------------------------------- ----------------------- \ D | any non-digit single character ------------------------------------------------------. | any character except line break | equivalent to [a-zA-Z0-9 _] character \ w | equivalent to [^ a-zA-Z0-9 _] ---------------------------------------------------------------- \ s | equivalent to [\ f \ n \ r \ t \ v] ------------------------------------------------------ \ s | equivalent to [^ \ f \ n \ r \ t \ v] -- -------------------------------------------------- \ X | \ 0 | ------------------------------------------------------ + | matches one or more character sets in combination * | matches 0 or multiple character sets in combination ------------------------------------------------------? | 0 or 1 character ------------------------------------------------------------ {} | repeat matching between the start and end of a word \ <| only matching the start of a word (not supported by many tools) matching \> | match only the end of a word (not supported by many tools) ^ | start of a matching string $ | end of a matching string ------------------------------------------------ ------ () | Subexpression ------------------------------------------------------? = | Forward search, find the content that appears after the matching text ------------------------------------------------------? <= | Backward Search, search for the content that appears before matching the text | logic or operation ---------------------------------------------------- \ e | End \ L or \ U conversion character \ L | convert the subsequent characters to lowercase until \ e is encountered convert \ L | convert the next character to lowercase ---------------------------------------------------- \ u | convert the next character to uppercase until \ e letter \ u | converts the next character to uppercase letter ------------------------------------------------------------------------------------------------------------
3. Advanced Application 3.1: backtracing Reference refers to the subexpression defined in the first half of the pattern. \ 1 is the first subexpression, \ 2 is the second subexpression, and so on. A backtracing example: it is required to match the continuously appearing word text in the text: hello worldbye byesmall is beautiful Regular Expression :( \ W +) [] + \ 1 Note: backend references can only be used to reference subexpressions in the mode. backend references generally use \ 1 to represent the first subexpression, while \ 0 to represent the entire regular expression. 3.2 The branch condition in the condition branch regular expression refers to several rules. If any rule is satisfied, it should be regarded as a match. The specific method is to use | to separate different rules. Example: The expression 0 \ D {2}-\ D {8} | 0 \ D {3}-\ D {7} matches two phone numbers separated by a font size: one is a three-digit area code, eight-digit Local Code (for example, 010-12345678), one is a four-digit area code, and a seven-digit local code (0376-2233445). Note: when using the branch condition, note the order of each condition because each condition is tested from left to right when the branch condition is matched. If a branch is satisfied, we will not care about other conditions. 4. How many regular expression instance urls: https? : // [-\ W.] + (: \ D + )? (/([\ W/_.] *)? IP Address: (\ D {1, 2}) | (1 \ D {2}) | (2 [0-4] \ D) | (25 [0-5]) \.) {3} (\ D {1, 2}) | (1 \ D {2}) | (2 [0-4] \ D) | (25 [0-5]) Email address: (\ W + \.) * \ [email protected] (\ W + \.) + [A-Za-Z] + HTML Note ://. * Regular Expression learning requires more exercises every day, including the use of Emacs and grep at ordinary times.

Regular Expression notes

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.