Regular expression (2)

zhaozj2021-02-16  47

In the previous article, some of the basic concepts of preliminary regular expressions have been introduced. I believe that many people have aware of the basic knowledge of regular expressions. Next, we combine some actual programming examples to cover up the regular expression. effect. First, let's take a few practical examples first: 1. Verify that the input character is all English characters JavaScript: var = "^ // w $";

Var RE = New Regexp (EX, "I");

Return Re.Test (STR);

VBScript

Dim Regex, Flag, EX

EX = "^ / w $"

SET Regex = New Regexp

Regex.ignorecase = TRUE

Regex.global = TRUE

Regex.pattern = EX

Flag = regex.test (STR)

C # system.string ex= @ "^ / w $";

System.Text.RegularExpressions.Regex reg = new regex (ex); BOOL flag = reg.ismatch (str);

2. Verify the mail format C # system.string ex= @ "^ / w @/w /w ";

System.Text.RegularExpressions.Regex reg = new regex (ex);

Bool flag = reg.ismatch (str);

3. Change the date of the date (replacing the date form of mm / dd / yy) C # with DD-MM-YY

String MDYTODMY (String Input)

{

Return Regex.Replace (Input,

"// b (? // D {1, 2}) / (? // d {1, 2}) / (? // D {2, 4}) // b ",

"$ {day} - $ {month} - $ {year}");

} 4. Extract protocol and port number C # from URL

String Extension (String URL)

{

Regex r = new regex (@ "^ (? / w ): // [^ /] ? (? : / d )? /",

Regexoptions.compiled;

Return R.Match (URL) .result ("$ {Proto} $ {port});

} The example here may be that some regular expressions we usually encounter in web pages, especially in the first example, give the implementation of different languages ​​such as JavaScript, Vbscript, C #, everyone is not difficult to see For different languages, the regular expression is not different, but the implementation of the regular expression is different. And how to play the public, but also to see the support of the class. (Excerpted from MSDN: Microsoft .NET Framework SDK provides a large number of regular expression tools that enable you to efficiently create, compare, and modify strings, and quickly analyze a lot of text and data to search, remove and replace text mode. Ms- Help: //ms.vscc/ms.msdnvs.2052/cpgenref/html/cpconregularexpressionsLANGuageElements.htm) Let's analyze these examples one by one: 1-2, these two examples are simple, just simple verification strings that match the regular rules The format specified in the expression, the syntax used in it, has been introduced in the first article, and it will be a simple description. The expression of the first example: ^ / w $ ^ - indicates that the qualified match begins with the start of the string

/ w - means matching English characters

- means 1 or more times in the matching character

$ - indicates that the match is over the end of the string end.

Verify strings such as AsgasDFS

The expression of the second example: ^/w @/w ./w

^ - Indicates that the qualified match begins with the start of the string

/ w - means matching English characters

- means 1 or more times in the matching character

@ - Match ordinary character @

- Match a normal character. (Note. For special characters, therefore add / translate)

$ - indicates that the match is over the end of the string end.

Verify that the format of Dragontt@sina.com

In the 3rd example, use the replace, so we will first look at the definition of the replacement in the regular expression: (ms-help: //ms.vsccc/ms.msdnvs.2052/cpgenref/html/cpconsubstitudes.htm )

replace

character

meaning

$ 123

Replace the last sub-string that matches the group number 123 (decimal).

$ {name}

Replace the last sub-string of matching (? ).

$$

Replace a single "$" character.

$ & &

Replace a copy of the itself completely matches itself.

$ `

Replace all text of the input string before matching.

$ '

Replace all text of the entered input string.

$

Replace the final captured group.

$ _

Replace the entire input string.

Packet structure (ms-help: //ms.vscc/ms.msdnvs.2052/cpgenref/html/cpcongroupingconstructs.htm)

Group construct

definition

()

Capture the matching sub-string (or non-capture group; for more information, see the EXPLICITCAPTURE Options in the regular expression option.) Use () capture to start automatic numbers from 1 according to the order of the left bracket. The first capture of capture element numbered zero is the text that matches the entire regular expression pattern.

(? )

Capture the matching substring to a group name or number name. A string for NAME cannot contain any punctuation, and cannot begin with a number. You can use single-quoted replacement angle brackets, such as (? 'Name').

(? )

Balance group definition. Delete the definition of previously defined Name2 groups and store the interval between the previously defined Name2 group and the current group in the Name1 group. If the NAME2 group is not defined, the match will be back. Since the last definition of the deletion of Name2 displays the previous definition of Name2, the configuration allows the Name2 group of capture stacks to be used as a counter to track nested structures (such as parentheses). In this configuration, NAME1 is optional. You can use single-quoted replacement angle brackets, such as (? 'Name1-Name2'). (?:)

Non-capture groups.

(? IMNSX-IMNSX:)

Apply or disable the options specified in the sub-expression. For example, (? I-S:) will open uncountment and disable a single line mode. For more information, see the regular expression option.

(? =)

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.

(?>)

Non-retractable expression (also known as greedy expressions). The sub-expression is only completely matched once, and then participate in the backtrack. (That is, the sub-expression is only matched with a string that can be individually matched by the sub-expression.)

We are still a simple understanding of these two concepts: Group construct: The most basic configuration is (), part of the left and right parentheses, is a packet; further grouping is like: (? The grouping method, this method is different from the first way, which is naming the part of the packet, so that information can be obtained by naming the group; (also a packet such as (? =), Etc. Construction, the example of our example is not used, next time we introduce) Replacement: The above mentioned two basic construction packet modes () and (? ), through these two packets, we You can get matching results such as $ 1, $ {Name}.

This way, may be conceptually blurred, we still combine the above example: The regular expression of the third example is: // b (? // D {1, 2}) / (? < Day> // d {1,2} / (? // D {2,4}) // b

(Explan, why is it // here: here is an example of the C #, in the C # language / translation character, you want to use // not translated in the string, you need to use // or the beginning of the entire string Plus @ 标,, ie equivalent

@ "/ B (? / d {1, 2}) / (? / d {1, 2}) / (? / D {2,4} / b")

/ b - is a special case. In the regular expression, the / b represents the word boundary (between / w and / w characters) in addition to the retracted character in the [] character class. In the replacement mode, / b always indicates the retracter.

(? / D {1, 2} - Constructs a group named MONTH, this packet matches a number of lengths 1-2 / - matching ordinary / characters

(? / d {1, 2} - Constructing a group named DAY, this packet matches a number of length 1-2

/ - Match ordinary / characters

(? / D {2, 4} / b ") - Constructs a group called Year, this packet matches a number of length 2-4

I can't see the role of these groups, we will look at this sentence.

$ {day} - $ {month} - $ {year}

$ {day} - Get information after the top-named DAY group matches

- - Ordinary - character

$ {month} - get information after packet matching Month as constructed above

- - Ordinary - character

$ {year} - Get the information of the packet matching of the above constructed Year

for example:

Alternatively replace the method of using an example of using an example of 04/02/2003

(? / D {1, 2} group will match 04 by $ {month} to this match value

(? / d {1, 2}) packets will match 02 by $ {day} to get this matching value

(? / D {1, 2}) Packet will match to 2003 by $ {year} to get this match value

After understanding this example, we are looking at the fourth example.

Regular ^ (? / w ): / / [^ /] ? (? : / d )? //

^ - Indicates that the qualified match begins with the start of the string

(? / w ) - Construct a packet named Proto, match one or more letters

: - Ordinary: Character

// - Match two / characters

[^ /] - means that this is not allowed / character

? - Indicates that specifies to use repetitions as little as possible but at least one match

(? : / d ) - Construct a packet called Port, matching shapes such as: 2134 (colon one or more numbers)

? - Indicates that the matching character appears 0 times or 1 time

/ - Match / character

Finally, get the matching content of two packet constructs through $ {prooto} $ {port}.

(References for regex objects

MS-help: //ms.vscc/ms.msdnvs.2052/cpref/html/frlrfsystemtextregularestXpressionsRegexMemberstopic.htm)

Ok, a few examples of this introduction, I also have a lot, I hope everyone will gain, next, in some special requirements, further explore the implementation of regular expressions.

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

New Post(0)