Character matching syntax:
character Syntax |
Grammar explanation |
Syntax Examples |
\d |
Matching digits (0~9) |
' \d ' matches 8, does not match 12; |
\d |
Match non-numeric |
' \d ' matches C, does not match 3; |
\w |
Match any single word character |
' \w\w ' matches A3, does not match @3; |
\w |
Matching non-single character |
' \w ' matches @, does not match C; |
\s |
Match White-space characters |
' \d\s\d ' matches 3 D, does not match ABC; |
\s |
Match non-null characters |
' \s\s\s ' matches a#4, does not match 3 D; |
. |
Match any character |
' ... ' matches a$ 5, does not match line-wrapping; |
[...] |
Match any character in parentheses |
[b-d] matches B, C, D, and does not match E; |
[^ ...] |
Matching non-bracket characters |
[^b-z] matches a, does not match the b-z character; |
Repeat match syntax:
repeating Syntax |
Grammar explanation |
Syntax Examples |
N |
Match N-th characters |
\d{3} matches \d\d\d, does not match \d\d or \d\d\d\d |
{N,} |
Match n times and N times above |
\W{2} matches \w\w and \w\w\w above, does not match \w |
{N,m} |
Match n times above M times |
\s{1,3} matches \s,\s\s,\s\s\s and does not match \s\s\s\s |
? |
Match 0 or 1 times |
5? Matches 5 or 0, does not match non 5 and 0 |
+ |
Match one or more times |
\s+ matches more than one \s and does not match more than one \s |
* |
Match more than 0 times |
\w* matches more than 0 \w, does not match non n*\w |
Character positioning Syntax:
repeating Syntax |
Grammar explanation |
Syntax Examples |
^ |
Locate back mode start position |
|
$ |
The previous mode is at the end of the string |
|
\a |
Front mode start position |
|
\z |
Front mode End Position |
|
\z |
Front mode end position (before wrapping) |
|
\b |
Match a word boundary |
|
\b |
Match a non-word boundary |
|
Escape matching syntax:
Escape Syntax |
involving characters (grammar interpretation) |
Syntax Examples |
"\" + Actual character |
\ . * + ? | ( ) { }^ $ |
Example: \ match character "\" |
\ n |
Match Line Wrap |
|
\ r |
Match carriage return |
|
\ t |
Match Horizontal Tab |
|
\v |
Match Vertical Tab |
|
\f |
Match page Change |
|
\nnn |
Matches a 8-in-system ASCII |
|
\xnn |
Matches a 16-in-system ASCII |
|
\unnnn |
Match 4-Uniode 16-binary |
|
\c+ Capital Letter |
Match Ctrl-Capital |
For example: \cs-matching Ctrl+s |
Example:
Read the file contents and analysis, the file format is as follows:
String[] lines = File.ReadAllLines (Server.MapPath ("Type.txt"), Encoding.UTF8); Regex Myregex = new Regex (@ ' ([\w\w]+) ' [\w\w]+ ' ([\w\w]+) ' [\w\w]+ ')]; Match match; foreach (string line in lines) { Response.Write (line + "<br/>"); Match = Myregex. Match (line); if (match. Success) { Response.Write ("Case \". "+match.") GROUPS[1]. Value + "\:<br/>");//Match 1th Response.Write ("ContentType = \" "+match.) GROUPS[2]. Value + "\;<br/>break;<br/>");//Match 2nd
} } |
The results of the run:
So, the regular expression is very powerful, especially when the batch format is very convenient to use.
Report:
Replace the keyword in the HTML code, but not the contents of the IMG a tag:
Html=regex.replace (html,@) (<! ( ]*?) | <a\b[^>]*?>[^<>]*?) (Design | skills) "," <a href=\ "http://www.test.com\" >$2</a> ");
Repeat what you want to match:
var txt= "[cq:image,file=a7d637e6f29f199d7d2addeb2a1d21b4.png][cq:image,file= A7d637e6f29f199d7d2addeb2a1d21b5.png]adad ";
Regex reg=new Regex (@ "\[cq:image,file= (? <filename>[^\s\r\n\]]+) \]", regexoptions.ignorecase);
MatchCollection matches = Reg. Matches (TXT);
if (matches. count>0) {
matches. Count.dump ();
foreach (match match in matches)
{
match. groups["FileName"]. Value.dump ();
}
Else "Match failed." Dump ();
More regular expression references: http://blog.csdn.net/dl020840504/article/details/8880603