Regular expression (3)

zhaozj2021-02-16  58

Title Regular Expression (3) Dragontt (original) Keyword regular expression

In the previous article, the basic syntax of the regular expression, as well as some simple examples. But these are not all of the issues we will encounter, and we have to write some more complex regular expressions to solve our practical problems. Here, I will take a few questions first, then we use the knowledge of regular expressions by using the regular expression. 1. Equipped with one of the two conditions, for example: is a pure number or pure character 123 (TRUE), Hello (TRUE), 234.Test23 (False) 2. To get a character combination that does not start with a number, you can get how and you, don't, do, do, do

3. DO and DO 4 are obtained in the character combination of characters starting. To get a combination of characters that do not end with numbers or the above situation, to get HO, Do, Yo, DO 5. Get the same superior example with the character combination of the number end, to obtain HO, DO, YO, DO 6. Not allowed to appear in the simultaneous example of AB: Nihaoma (True), ABOVE (FALSE), AGOODBOY (TRUE)

Below we started to solve these problems: The first one: Establishing this requirement to represent a common requirement, let's take a look at this table replacement structure

Replacement structure

definition

|

Matching any of the terms separated by a | (vertical strip) character; for example, Cat | Dog | Tiger. Use the leftmost success match.

(? (Expression) YES | NO)

If the expression matches this location, match the "YES" section; otherwise, match the "NO" section. "NO" section can be omitted. The expression can be any effective expression, but it will become zero width assertions, so the syntax is equivalent to (? (? = Expression) YES | NO). Note that if the expression is the name of the naming group or the capture group number, the replacement structure will be interpreted as a capture test (described in this table). To avoid confusion in these cases, you can explicitly spell an internal (= expression).

(? (Name) Yes | NO)

If the name capture string matches, match the "YES" section; otherwise, match the "NO" section. "NO" section can be omitted. If a given name does not correspond to the name or number of the capture group used in this expression, the replacement structure will be interpreted as an expression test (described in the previous form of this table).

(MS-Help: //ms.vscc/ms.msdnvs.2052/cpgenref/html/cpconalternationconstrunts.htm) In this table, we see that in order to solve this type of problem, define | The relationship is like a common or operator. Now let's take a look at how to use | to solve our problem. 1. First write expressions for selectable expressions: a) Pure Number - [0-9] * b) Pure letters - [A-ZA-Z] * 2. Use the optional condition | Connecting is what we need ^ [0-9] * $ | ^ [A-ZA-Z] * $ (here I specially add ^ and $ qualified characters, this If the verification string is fully compliant, it is necessary. If these two qualifiers are not added, interested friends can try their own effect. Four questions later, actually a class, so we put them Trouble together. Next we will solve the second to fourth questions: First, let's review the group construct introduced last time:

(? =)

Zero width is predicting the first line assertion. Conveuing only when the sub-expression matches the right side of this location. For example, / w (? = / D) matches the word followed by the number without matching the number. This construct does not retrore.

(?!)

Zero width negative prediction first line assertions. You can continue to match only if the sub-expression does not match the right side of this location. For example, / b (?! Un) / w / b matches the word not starting with UNN.

(? <=)

Zero width is reviewing the assertion. Conveuing only when the sub-expression matches the left side of this location. For example, (? <= 19) 99 matches an example of 99 followed by the 19. This construct does not retrore.

(?

The zero width is negative after review. Match only when the sub-expression does not match the left side of this location.

It can be seen that these four rules of this table can solve our problems. @ _ @ First solve our problem and say: Second case: To get a combination of characters not starting with numbers (?

(?

[A-ZA-Z] {2,} - Description Match 2 or more letters

(Note: This is a practical practice, because, according to our logic how2234do> you234do's O letter is also in line with it, but this is not what we want, of course, there are other solutions, can be based on actual The situation is handled, here is to explain this method @ _ @)

Third Cases: Get the character combination of characters starting (? <= / D) [A-ZA-Z]

(? <= / d) - Character that is limited to the beginning of the number matches

[A-ZA-Z] - Description Match 1 or more letters

The fourth example: To get a character combination without numbers [A-ZA-Z] (?! / D)

[A-ZA-Z] - Description Match 1 or more letters

(?!! / d) - Limited the letters that are not ending the numbers

Section 5: Get the character combination of numbers [A-ZA-Z] (? = / D)

[A-ZA-Z] - Description Match 1 or more letters

(? = / d) - the letter that is limited to the end of the number matches

Sixth Case: Do not allow the character to appear simultaneously ^ (?!. *? Ab). * $

(?!. *? ab) - Limited the character that does not allow AB-connected characters

* - any character

转载请注明原文地址:https://www.9cbs.com/read-18530.html

New Post(0)