Regular expressions (Regular expressions) are a set of grammar matching rules for various languages, such as Perl. NET and Java both have their
The corresponding shared regular Expression class library. In. NET, this class library is called a regex.
Here are a few static methods under the Regex:
Escape: Escapes the escape character in a regex in a string;
IsMatch: If an expression matches in a string, the method returns a Boolean value;
Match: Returns an instance of match;
Matches: Returns a series of match methods;
Replace: Replaces the matching expression with a replacement string;
Split: Returns a series of strings determined by an expression;
Unescape: Escape character in string is not escaped.
The following code example creates an instance of the Regex class and defines a simple regular expression when the object is initialized. Declare a Regex object variable: Regex objalphapatt, and then create an instance of the Regex object and define its rules: Objalphapatt=new regex ("[^a-za-z]");
The IsMatch method indicates whether the regular expression specified in the Regex constructor finds a match in the input string. This is one of the most common methods we use when using C # regular expressions. The following example illustrates the use of the IsMatch method:
if (!objalphapatt.ismatch ("Testismatchmethod"))
Lblmsg.text = "Match succeeded";
Else
Lblmsg.text = "Match not successful";
The result of this code execution is "match succeeded"
if (! Objalphapatt.ismatch ("testisMatchMethod7654298"))
Lblmsg.text = "Match succeeded";
Else
Lblmsg.text = "Match not successful";
The result of this code execution is "match not successful"
The split method splits the input string into a substring array by the location defined by the regular expression match. For example:
Regex r = new Regex ("-"); Split on hyphens.
String[] s = r.split ("First-second-third");
for (int i=0;i<s.length;i++)
{
Response.Write (s[i]+ "<br>");
}
The result of the execution is:
First
Second
Third
The match method searches the input string for a match for a regular expression, and the match method of the Regex class returns the Match object, which represents the result of the regular expression match operation. The following example shows the use of the match method and returns the group object using the Match object's Group property:
string text = @ "public string Testmatchobj string s string Match";
String Pat = @ "(\w+) \s+ (string)";
Compile the regular expression.
Regex r = new Regex (PAT, regexoptions.ignorecase);
Match the regular expression pattern against a text string.
Match m = r.match (text);
int matchcount = 0;
while (m.success)
{
Response.Write ("Match" + (++matchcount) + "<br>");
for (int i = 1; I <= 2; i++)
{
Group g = m.groups[i];
Response.Write ("Group" +i+ "= '" + G + "'" + "<br>");
capturecollection cc = g.captures;
for (int j = 0; J < cc. Count; J + +)
{
Capture C = cc[j];
Response.Write ("Capture" +j+ "= '" + C + "', position=" +c.index + "<br>");
}
}
m = M.nextmatch ();
}
The result of this example operation is:
Match1
group1= ' Public '
capture0= ' public ', position=0
Group2= ' String '
Capture0= ' string ', position=7
Match2
group1= ' Testmatchobj '
Capture0= ' Testmatchobj ', position=14
Group2= ' String '
Capture0= ' string ', position=27
Match3
Group1= ' s '
Capture0= ' s ', position=34
Group2= ' String '
Capture0= ' string ', position=36
The following is the format of finding a repeating letter in a string using a regular expression:
Pattern |
Description |
\b |
Start the match at a word boundary. |
(? <word>\w+) |
Match one or more word characters up to a word boundary. Name This captured group Word. |
\s+ |
Match one or more white-space characters. |
(\k<word>) |
Match the captured group is named Word. |
\b |
Match a word boundary. |
The following are some meta-characters of the regular expression:
Pattern |
Description |
^ |
Start at the beginning of the string. |
\s* |
Match zero or more white-space characters. |
[\+-]? |
Match zero or one occurrence of either the positive sign or the negative sign. |
\s? |
Match Zero or one white-space character. |
\$? |
Match zero or one occurrence of the dollar sign. |
\s? |
Match Zero or one white-space character. |
\d* |
Match zero or more decimal digits. |
\.? |
Match zero or one decimal point symbol. |
\D{2}? |
Match the decimal digits zero or one time. |
(\d*\.? \D{2}?) {1} |
Match the pattern of integral and fractional digits separated by a decimal point symbol at least one time. |
$ |
Match the end of the string. |
The regular expression of 4.c# learning &®ex