Regular Expression-advanced (2)

Source: Internet
Author: User

I will continue to talk about the application of regular expressions in advanced forms. This is also the last regular expression article. I hope that the three regular expression articles in this blog will help most of my friends understand the regular expression usage rules, I hope you can practice more and improve your work efficiency.

If you are not familiar with regular expressions or do not remember some basic things, you are strongly advised to read the first two articles first, because the three articles are sorted from basic to advanced.

Getting started with regular expressions

Regular Expression (1)


1. Backtracking reference

 

Backend Reference refers to the subexpression that is defined in the first half of the pattern. backend reference can only be used to reference the subexpression in the pattern. The following is an example of backend reference.
The 8th rows that can only match H1 8th rows are invalid.
\ 1 \ 2 indicates the first two subexpressions in the mode, and so on. \ 3 indicates the first two subexpressions.
In the replacement mode, the \ needs to be changed to $ according to the implementation of different languages, but in Vim, it seems that after the replacement operation is not required, convert \ to $ as follows:
FinishedThe following is the correct execution result: the metacharacters \ e used for case-insensitive conversion \ L and \ U conversion \ l convert the characters between \ L and \ e to lowercase \ U to convert the characters between \ L and \ e convert character to uppercase \ l convert the next character to lowercase \ U convert the next character to uppercase \ U \ l can only convert the next character (or subexpression) to convert to uppercase or lowercase \ U \ L, you can convert all the subsequent characters to uppercase or lowercase until the replacement operation is performed after \ e. first, we will explain this concept. The Textbook version: "Search for content before and after a location" is shown in the following example: <title> This is a title </title> uses regular expressions to match the title. The matched string contains only the characters "this is a title, there is also the <title> </title> above and below. At this time, if we only want
"This is a title", so what we need is "search before and after. In terms of syntax, a forward search mode is actually? The child expression starting with =. The text to be matched follows =, but does not appear in the matching result. Forward lookup character :? =I tried it and found that
Not supported by VIMThis is depressing again. We will use
Notepad ++To hide it.
Note that the previous matching result is not used? =. Search backward :? <=? <= And? = Is used basically the same: it must be used in a subexpression and followed by the text to be matched. What should we do if we want to remove "$" from the matching result?

If you simply win the prize from the regular expression $, can you? Obviously, this is not what we want.
Let's take a look at backward matching? <= Efficacy. This seems to be what we need.
Warning: The length of the forward lookup mode is variable. They can contain metacharacters such as. And +, so they are flexible. The backward lookup mode can only be a fixed length, which is a limit applied to almost all regular expressions.

Next, let's look at an example that combines forward and backward lookup:

Select non-forward and backward lookup. Use it !, ^ Cannot be used. Before and after the search operator (? =) Forward lookup (?!) Negative forward lookup (? <=) Forward lookup (? <!) Negative backward Lookup

Note the preceding two
Different Regular ExpressionsMatched
Different Results

 

 

3. Embedding conditions:

 

Warning: not all regular expression implementations support conditional processing. Vim does not support the following :? Match the previous character or expression-if it exists? = And? <= Match the text above or below -- if it exists Backtracking referenceCondition: a single expression is allowed only when the previous expression search is successful. ? (Backreference) True-RegEx | false-RegEx ?, This indicates that the backreference in the condition brackets is a backtrace reference, is the subexpression number true-RegEx is a subexpression that is executed only when the backreference exists. False-RegEx is a subexpression that is executed only when the backreference does not exist. the regular expression used above: (<[AA] \ s + [^>] +> \ s *)?, Match A <A> or the <A> label and any attributes .? This label is optional.
(<[AA] \ s + [^>] +> \ s *)?,
Match A label (case-sensitive) and any attributes
(? (1) \ s * </[AA]>). The point is, this is a backtracing reference condition ,? (1) If the first backend reference (<A> tag) exists, use the following expression (\ s * </[AA]>).

Search before and afterCondition: only when a forward or backward search operation is successful can a forward search condition be used. Syntax :? (Condition) True-RegEx | false-RegEx

We use the U.S. zip code as an example. There are two formats. Others are invalid. 1) ZIP format in the form of 12345 2) we only want to match the two valid formats of zip + 4 in the format of 12345-6789. The Invalid format should not appear in the matching results.
The "4th-" in the fifth row also appears in the matching result. No !!! Now it looks okay. Parse the regular expression \ D {5 }(? (? =-)-\ D {4}) \ D {5}. This is very simple, that is, matching five numbers (
? (? =-)-\ D {4}) first ?, It indicates that this is a forward and backward search condition ,? =-Match a hyphen '-', but it is not consumed. If the condition is met (match to a hyphen ), then-\ D {4} matches the four digits after the hyphen. In this way, 33333-is excluded from the final matching result.

The level is limited. If a friend finds an error, please leave a message.

 

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.