C # - Regex and Match classes, code applications and instances 114

#Headline Creation Challenge#Using in C #RegexAndMatch,

#Headline Creation Challenge#

Using in C #RegexAndMatch,,And,SystemTextRegularExpressionsUnder the namespace

Regex classA class used to handle regular expressions;Match classUsed to obtain the results of matching regularization

Regex class

Regex class;:

Static method: Net will automatically cache regular expressions, but when using the same regular expressions, it will not recompile Generally, it will cache 15 regular expressions

Instantiation method: Every instantiation will recompile the regularization

Regex class

Regex IsMatch(); Determine whether a String matches a regular expression

Regex Match(); Extract a subString that matches a regular expression from a certain String (only one can be extracted)

Regex Matches(); Extract all matching Strings from the String

Regex Replace()// String replacement, replacing all Strings that match regular expressions with corresponding Strings

Static mode//1) Write matching regular expressionsStringStrReg=^ 410d {15} $;//Static method for calling judgment (matching rules, string to be verified)BoolA=Regex IsMatch (strReg,41069608095689898769NewRegex (strReg);BoolB=reg IsMatch (str);

Three Basic Applications of Regular Expressions

1 Determine whether the string matches, with the format Regex IsMatch ("string", "regular expression");

2 String extraction: Regex Match ("string", "regular expression")// Only one can be extracted

String extraction: RegexMatches ("string", "regular expression")// Extract all matching strings

3 string replacement: RegexReplace ("string", "regular expression", "replaced content");

Match classThe regular result used for obtaining is Regex Match(); Regex Matches(); The return value types of methods mainly include:

1) Match value: Retrieved regular content 2) Match Index: Actual subscript of content 3) Match Length: Content length

3) Match Groups: All capture groups The default Group [0] is all captured content If the same capture group has multiple contents, the last one will be taken

4) Match Captures If a capture group has multiple contents, Group represents the last one, and Captures represents the entire content of the capture group

5) Match Collection: An iterable regular set 6) Match Success: Determine if a match exists

7) Match Next: If multiple results are matched, used to return the next 8) Match Result: used to replace

Regular Application Example

1) IsMatch is used to determine whether a string matches a regular expression and returns a Boolean value

Returns true as long as a substring with 3 numbers matches within a string

2) Match is used to extract substrings from a string that match a regular expression It only extracts the first string that matches, usually without adding ^ (beginning) and $(ending) If added, it indicates an exact match

As mentioned in the previous section, the meaning of a single character when combined is to match a period and at least one previous character or string other than n

3) Matches is used to extract all substrings in a string that match a regular expression; Pay attention to the greedy mode

The group subscript 0 is used to extract matching regular expressions; Subscript 1 is the extracted group, which matches the string in parentheses

Matches extracts groups based on the parentheses in regular expressions, even if the original intention of the parentheses is to change priority rather than an option for extracting groups

4)Determine if it is a legal postal code (6 digits)

Only verify if the postal code is a six digit number

[0-9] "represents any character from 0 to 9," {6} "represents that the preceding character must be matched 6 times, and" [0-9] "can be represented by d Therefore," [0-9] {6} "is equivalent to" d {6} " Therefore, str1 string validation returns true for any 6 digit match in the string

@"^d{6}$"And"^d{6}$"And,str2="100088"@"^d{6}$"And"^d{6}$"

str1Andstr2true,,,,^And$

5) Determine whether the input string is a valid email address

Simple verification of email address

The email address is roughly as follows: qwoieu@163com perhaps qwoieu@163comcn etc

The process of writing regular expressions is to analyze their approximate composition and then separately write the simple regular expressions in the example questions, which are divided into three parts

1) [a-zA-Z0-9_]+means to match any 0-9 number and uppercase and lowercase letter a-z plus an underscore, and complex ones plus others The plus sign means that the characters in the brackets must appear once or more

2)@[a-zA-Z0-9]+ @,AndAnd1)

3)([a-zA-Z]+)+ [a-zA-Z]+And1)

AndThe meaning is different,Y becauseThere are special meanings in regular expressions, so if you really want to expressThen it needs to be transferred to

([a-zA-Z]+)+comcn

,Regex class


Disclaimer: The content of this article is sourced from the internet. The copyright of the text, images, and other materials belongs to the original author. The platform reprints the materials for the purpose of conveying more information. The content of the article is for reference and learning only, and should not be used for commercial purposes. If it infringes on your legitimate rights and interests, please contact us promptly and we will handle it as soon as possible! We respect copyright and are committed to protecting it. Thank you for sharing.(Email:[email protected])