Regular expression
Regular Expression is an important concept in computer science. It uses a mathematical algorithm to solve the problems of text retrieval and matching in computer programs. Regular Expression language is a language specially used for string processing. It is supported in many languages and c# is no exception
Two functions:
Retrieve: Retrieve the desired part of a string
Match: Determines whether a string conforms to the filtering logic of a regular expression
Common usage: Check whether the password or email format entered by a user is valid
A regular expression is a text pattern consisting of ordinary characters and special characters. Special characters are metacharacters
The \ escape character is used to represent \
The @ symbol, which adds the @ character to a regular expression to indicate that it does not allow the compiler to parse the escape characters in the regular expression
locator
^ Matches the start of the string
$matches the end position of the string
\b Matches the boundary of a word
\B Matches non-word boundaries
Example: Match start and end
string str = "World"; Replace(STR, "^", "Hello "); // Match the starting position with the specified string string res = regx.replace (STR, "^", "Hello "); Replace(STR, "$", "!" ); / / o World!Copy the code
Matches any character except newline
\w Matches letters, digits, underscores, and Chinese characters (upper and lower case letters, digits from 0 to 9, and underscores).
\W \W complement (except “upper and lower case letters, 0-9 digits, underscore _”)
\s Matches any whitespace characters (including newline /n, carriage return /r, TAB /t, vertical TAB /v, feed /f)
Complement of \S \S (except for the characters defined by \S)
\d Matching digits (0-9 digits)
\D denotes the complement of \D (except for 0-9 digits)
Example: Only numbers are allowed
Regex.IsMatch(input, @"^\d*$");
Copy the code
[ABC] matches a, B, or C
[a-c] matches the characters between A and C
[^x] matches any character other than x
Example: Replace any character except ace with *
string str = "abcdef"; string pattern = @"[^ace]"; Replace(STR, pattern, "*"); string res = regx.replace (STR, pattern, "*"); // replace any character except ace with * // The result is a* C *e*Copy the code
qualifiers
* indicates that zero or multiple occurrences occur
+ indicates one or more occurrences
? Indicates 0 or 1 occurrence
{n} represents n occurrences, and n represents a non-negative integer
{n,} indicates at least n occurrences
{n,m} indicates n to m occurrences
Example: verify whether the entered QQ number is valid (note: QQ number is 5-12 digits)
string qq1 = "123", qq2 = "397458006",qq3="abc123456"; String pattern = @ "^ \ d {5, 12} $"; bool isMatch1 = Regex.IsMatch(qq1, pattern); //false bool isMatch2 = Regex.IsMatch(qq2, pattern); //true bool isMatch3 = Regex.IsMatch(qq3, pattern); //falseCopy the code
Choose a match
| characters, the two matching conditions by using the Boolean or operation
Example: Find numbers or letters
String STR = "123 "; string pattern = @"\d|[a-z]"; MatchCollection col = Regex.Matches(str, pattern); foreach (Match match in col) { Console.WriteLine(match); }Copy the code
The results for
grouping
Use parentheses ()
Example: Replace a single character twice with **
string str = "aabbaaacccaaaa"; string pattern = @"a{2}"; string res = Regex.Replace(str, pattern, "**"); **bb**accc****Copy the code
Example: Verify the IP4 address
string pattern = @"^(((2[0-4]\d|25[0-5]|[01]? \d\d?) \.) {3}(2[0-4]\d|25[0-5]|[01]? \d\d?) ) $";Copy the code
Related links:
Learning regular expressions
Common regular expressions