Regular Expression C #

Source: Internet
Author: User

First, let's take a look at several practical examples:
1. verify whether the input characters are
Javascript:
VaR EX = "^ \ 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 email 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 format (replace the date format of mm/DD/yy with the date format of DD-mm-yy)

C #

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 from URL

C #

String extension (string URL)

{

RegEx r = new RegEx (@ "^ (? \ W +): // [^/] +? (? : \ D + )? /",

Regexoptions. Compiled );

Return R. Match (URL). Result ("$ {proto }$ {port }");

}

The example here may be some of the regular expressions we usually encounter in Web development. Especially in the first example, we show how to use JavaScript, VBScript, C # and other implementation methods in different languages, it is not difficult to see that for different languages, regular expressions are no different, but the implementation classes of regular expressions are different. How to make full use of the regular expression is also dependent on the support of the Implementation class.

(From msdn: Microsoft. net Framework SDK provides a large number of regular expression tools, allowing you to efficiently create, compare, and modify strings, and quickly analyze a large amount of text and data to search, remove, and replace text patterns. MS-help: // Ms. VSCC/ms. msdnvs.2052/cpgenref/html/cpconregularexpressionslanguageelements.htm)

We will analyze these examples one by one:

1-2. The two examples are very simple. They simply verify that the string conforms to the format specified by the regular expression. The syntax used in this example is described in the first article.ArticleHave already been introduced, here is a simple description.

Expression for the 1st example: ^ \ W + $

^ -- Specifies that the match starts with the start of the string.

\ W-Indicates matching English characters

+ -- Indicates that a matching character appears once or multiple times.

$ -- Indicates that the matching character ends at the end of the string.

Verify the string like asgasdfs

Expression in the 2nd example: ^ \ W + @ \ W +. \ W + $

^ -- Specifies that the match starts with the start of the string.

\ W-Indicates matching English characters

+ -- Indicates that a matching character appears once or multiple times.

@ -- Match common characters @

\.-Match common characters. (note. It is a special character, so \ translation must be added)

$ -- Indicates that the matching character ends at the end of the string.

Validate the format of a message like a dragontt@sina.com

Replace is used in the 3rd example. Therefore, let's take a look at the replace definition in the regular expression:

(MS-help: // Ms. VSCC/ms. msdnvs.2052/cpgenref/html/cpconsubstitutions.htm)

Replace
Character
Description

$123
Replace the last substring matched by group number 123 (decimal.

$ {Name}
Replace (? ) The last substring that the group matches.

$
Replace a single "$" character.

$ &
Replace a copy that exactly matches itself.

$'
Replace all text of the input string before matching.

$'
Replace all text of the matched input string.

$ +
Replace the last captured group.

$ _
Replace the entire input string.

Group Structure
(MS-help: // Ms. VSCC/ms. msdnvs.2052/cpgenref/html/cpcongroupingconstructs.htm)

Group Structure
Definition

()
Capture matched substrings (or non-capturing groups). For more information, see the explicitcapture option in regular expression options .) Capture with () is automatically numbered starting from 1 according to the sequence of left parentheses. The first capture of zero element number is text that is matched by the entire regular expression pattern.

(? <Name>)
Capture the matched substring to a group name or serial number name. The string used for name cannot contain any punctuation marks and cannot start with a number. You can use single quotes to replace angle brackets, such (? 'Name ').

(? <Name1-name2>)
Balancing Group definition. Delete the definition of the previously defined name2 group and store the interval between the previously defined name2 group and the current group in the name1 group. If no name2 group is defined, the matching will be traced back. Since deleting the last definition of name2 displays the previous definition of name2, this construction allows the capture stack of the name2 group to be used as a counter to trace nested structures (such as parentheses ). In this construction, name1 is optional. You can use single quotes to replace angle brackets, such (? 'Name1-name2 ').

(? :)
Non-capturing group.

(? Imnsx-imnsx :)
Apply or disable the options specified in the subexpression. For example ,(? I-s:) will enable case-insensitive and disable single row mode. For more information, see regular expression options.

(? =)
0-width positive prediction first asserted. The child expression continues matching only when it matches the right side of the position. For example, \ W + (? = \ D) matches the word followed by a number instead of the number. This construction will not be traced back.

(?! )
0-width negative prediction first asserted. The child expression continues matching only when it does not match the right side of the position. For example, \ B (?! UN) \ W + \ B matches words that do not start with UN.

(? <=)
Assertion after the blank width is reviewed. The child expression continues matching only when it matches the left side of the position. For example ,(? <= 19) 99 matches the 99 instance following 19. This construction will not be traced back.

(?
Assertion after review with Zero Width and negative. The child expression continues matching only when it does not match on the left side of the position.

(?> )
Non-backtracking subexpression (also called greedy subexpression ). This subexpression only matches exactly once, and then does not participate in backtracking step by step. (That is, this subexpression only matches strings that can be independently matched by this subexpression .)

Let's take a brief look at these two concepts:

Group structure:

The most basic constructor is (). The Section enclosed in parentheses is a group;

Further grouping is like :(? <Name>). The difference between this method and the first method is to name the part of the Group so that information can be obtained through the group name;

(Is there a shape like (? =). We have not used the grouping structure in this example. We will introduce it next time)

Replace:

The two basic grouping methods () and (? <Name>). Through these two grouping methods, we can get the matching results, such as $1, $ {name.

In this case, the concept may be vague. Let's take the above example as an example:

In the third example, the regular expression is \ B (? \ D {1, 2 })/(? \ D {1, 2 })/(? \ D {2, 4}) \ B

(To explain why all of them are used together \: Here is the C # example. in C #, \ is a conversion character. If you want \ In a string to not translate, you need to use \ or add @ to the start of the entire string, that is, the above is equivalent

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

\ B -- is a special case. In a regular expression, \ B Represents the boundary (between \ W and \ W) except for the escape character in the [] character class ). In replacement mode, \ B always indicates the return character

(? \ D {1, 2})-creates a group named month, which matches a number with a length of 1-2.

/-- Match common/Character

(? \ D {1, 2}) -- construct a group named Day, which matches a number with a length of 1-2.

/-- Match common/Character

(? \ D {2, 4} \ B ") -- creates a group named year, which matches a number with a length of 2 to 4.

The role of these groups cannot be seen here. Let's look at this sentence.

$ {Day}-$ {month}-$ {year}

$ {Day}-obtain the matched information of the group named day constructed above.

--- Common-Character

$ {Month} -- Obtain the matched information of the group named month constructed above.

--- Common-Character

$ {Year} -- Obtain the matched information of the group named year constructed above.

For example:

Replace the following three methods in the Date Format: 04/02/2003

(? \ D {1, 2}) the Group will match to 04 and get this matching value from $ {month }.

(? \ D {}) the Group will match to 02 and get the matching value from $ {day }.

(? \ D {2003}) the Group will match to and get the matching value from $ {year }.

After learning about this example, it is very easy to look at the 4th examples.

Regularizedtype in 4th examples

^ (? \ W +): // [^/] +? (? : \ D + )? /

^ -- Specifies that the match starts with the start of the string.

(? \ W +)-construct a group named proto and match one or more letters.

: -- Common: character

// -- Match two/Character

[^/]-Indicates that the character is not allowed here.

+? -Indicates to use as few duplicates as possible, but at least one match is used.

(? : \ D +)-create a group named "Port". The matching format is 2134 (colon + one or more numbers)

? -It indicates that the matching character appears 0 or 1 time.

/-- Match/Character

Finally, use $ {proto} $ {port} to obtain the Matching content of the two grouping structures.

(For usage of the RegEx object, refer

MS-help: // Ms. VSCC/ms. msdnvs.2052/cpref/html/frlrfsystemtextregularexpressionsregexmemberstopic.htm)

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.