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. These two examples are very simple. They simply verify that the string conforms to the format specified by the regular expression. The syntax used is described in the first article, 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)