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
Such as: how2234do> you234do, I hope to get how and you not do, do, do
3. Get the character combination with numbers
In the above example, you get DO and DO
4. To get a combination of characters that do not end with numbers
Still the situation above, to get HO, DO, YO, DO
5. Get the character combination of numbers
In the same example, get HO, DO, YO, DO
6. Do not allow AB in characters to appear at the same time
Example: Nihaoma (True), ABOVE (FALSE), Agoodboy (True)
Below we started to solve these problems:
The first one: Established one of the two conditions.
This requirement may 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/cpConalternationConstructs.htm)
In this table, we see that in order to solve this type of problem, define the relationship of | to represent or the relationship, just 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 numbers - [0-9] *
b) Pure letters - [A-ZA-Z] *
2. Use optional conditions | Connecting is what we need
^ [0-9] * $ | ^ [A-ZA-Z] * $
(Here I have specially add ^ and $ qualifiers to two conditions, which is necessary when verifying whether the string is fully qualified, if it does not add these two qualifiers, interested friends can try themselves effect.
The following four problems are actually a class, so we put them together. Next, let's 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: To get a character combination that does not start with a number
(?
(?
[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 example: get a character combination at the beginning of the number
(? <= / 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 that is not ending
[A-ZA-Z] (?! / d)
[A-ZA-Z] - Description Match 1 or more letters
(?!! / d) - Limited the letters that are not ending the numbers
The fifth example: 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 AB in characters to appear simultaneously
^ (?!. *? ab). * $
(?!. *? ab) - Limited the character that does not allow AB-connected characters
* - any character
Introducing here, our question is also solved. Although the example is simple, but complex things are also based on a simple basis. In fact, the key to writing regular expressions is to be good at customization rules, describe the most concise correct words, and then write it with the syntax of the regular expression, you can rely on everyone to accumulate experience.