Net.
, .
,
,
.
1. .NET
2.
3.
4.
5.
6. Regex
o 6.1. Regex
o 6.2. Regex
o 6.3.
o 6.4.
7.
1. .NET
(regular expression, regexp) - ,
. :
(BRE - basic regular expressions) (ERE extended regular expressions). ERE BRE.
, .Net, Perl
( ).
.
.Net, ,
Perl
.
:
. , ,
. Net
( Posix ).
.
. ,
( Posix
, ).
.Net :
using System.Text.RegularExpressions;
:
Regex - .
Match -
.
MatchCollection - ,
.
Capture - .
Group - .
GroupCollection -
.
CaptureCollection -
.
,
.
2.
1 2,
Match MatchCollection.
, :
1. regex s 1:
private void button1_Click(object sender, EventArgs e)
{
string s = 1 1;
Regex regex = new Regex( 1 1);
Match match = regex.Match(s);
Text = "";
if (match.Success)
{
for (int i = 0; i < match.Groups.Count; i++)
{
Text += match.Groups[0].Value.ToString() + " ";
}
}
}
2. regex s 1:
Regex("a.b");
s = "a1ba2ba3ba4b"; : a1b
,
\n.
2
s = "a1ba2ba3ba4b"; : a1b a2b a3b
a4b
"." , "\."
Regex(@"a\.b");
s = "a1ba2ba3ba.b"; : a.b
Regex("a|b");
() s = "abcabcabcabc"; : a
2
s = "abcabcabcabc"; : a b a b a b a b
Regex("[abc]");
[-
-]
s = "abcdabcdabcdabcd"; : a
(
)
2
s = "abcdabcdabcdabcd"; : a b c a b c
abcabc
[^-
-]
Regex("[^abc]");
s = "abcdabcdabcdabcd"; : d
s = "abcdabcdabcdabcd"; : d d d d
Regex("^abc");
s = "abcdefabcdef"; : abc
s = "abcdefabcdef"; : abc
1
2
Regex("^bc");
Regex("def$");
s = "abcdefabcdef"; : def
s = "abcdefabcdef"; : def
1
2
Regex("(ab)");
(....)
s = "abcdefabcdef"; : ab ab
s = "abcdefabcdef"; : ab ab
Regex("ab?");
0 1
s = "aabbbcde"; : a
s = "aabbbcde"; : a ab
Regex("a?b?");
s = "aabbbcde"; : a
s = "aabbbcde"; : a ab b b
Regex("a?b");
s = "aabbbcde"; : ab
s = "aabbbcde"; : ab b b
Regex("ab*");
s = "aabbbcde"; : a
s = "aabbbcde"; : a abbb
Regex("a*b*");
0
s = "aabbbcde"; : aabbb
s = "aabbbcde"; : aabbb
Regex("a*b");
s = "aabbbcde"; : aab
s = "aabbbcde"; : aab b b
Regex("ab+");
s = "aabbbcde"; : abbb
s = "aabbbcde"; : abbb
Regex("a+b+");
s = "aabbbcde"; : aabbb
s = "aabbbcde"; : aabbb
Regex("a+b");
s = "aabbbcde"; : aab
s = "aabbbcde"; : aab
Regex("ab{2}");
{n}
s = "aabbbcde"; : abb
s = "aabbbcde"; : abb
Regex("ab{2,4}");
{n,m}
n m
s = "abbbcdeabbbbbcde"; : abbb
2
s = "abbbcdeabbbbbcde"; : abbb
abbbb
Regex("ab{2,}");
{n,}
s = "abbbcdeabbbbbcde"; : abb
2
s = "abbbcdeabbbbbcde"; : abbb
abbbbb
Regex("ab*?c");
*?
1
0
s = "abbbcdeabbbbbcde"; : abbb
,
2
s = "abbbcdeabbbbbcde"; : abbb
abbbbb
Regex("ab+?c");
+?
??
1
1
s = "abbbcdeabbbbbcde"; : abbb
,
2
s = "abbbcdeabbbbbcde"; : abbb
abbbbb
0 1 ,
Regex("ab??c");
s = "acdeabcdeabbcde"; : a
s = "acdeabcdeabbcde"; : ab
Regex(@"\w");
\w
( )
s
=
"abcd_~`!@#$%^&*()=+|:;\",.<>?/12345"; : A
s
=
"abcd_~`!@#$%^&*()=+|:;\",.<>?/12345"; : A b c d _ 1 2 3 4 5
Regex(@"\W");
\W
s
=
"abcd_~`!@#$%^&*() ( =+|:;\",.<>?/12345"; : ~
)
s
=
"abcd_~`!@#$%^&*()=+|:;\",.<>?/12345"; : ~ ` ! @ # $ % ^ & * ( )
-=+|:;",.<>?/
Regex(@"\d");
\d
s = "abcd12345"; : 1
s = "abcd12345"; : 1 1 2 3 4 5
Regex(@"\D");
\D
s = "abcd12345"; : a
2
s = "abcd12345"; : a b c d _ ~ ` ! @ #
$%^&*()-=+|:;",.<>?/
Regex(@"\s");
\s
\S
(, \f,
s = "ab cd"; : b c
\n, \r, \t, \v)
s = "ab cd"; : b c
Regex(@"\S");
(
, \f, \n, \r,
\t, \v)
s = "ab c\td"; : a
s = "ab c\td"; : a b c d
Regex(@"\bqw"); //
Regex(@"\bwe");
\b
(
:
- ;
- .)
s = "abcde"; : qw qw
Regex(@"ty\b"); //
Regex(@"rt\b");
Regex(@"\Bwe"); //
Regex(@"\qw");
\B
s = "abcde"; : we we
(
:
- ;
- .)
Regex(@"rt\B"); //
Regex(@"ty\B");
Regex(@"ab\A"); //
Regex(@"qw\A");
\A
\Z
""
Regex(@"ty\Z");
Regex(@"ty\n\z");
\z
1
""
2
s = "abcde qwerty qwerty"; : ty +
Regex(@"\G\d{3}");
1
\G
match.NextMatch()
Match.
while(match.Success)
{
for(int i = 0; i < match.Groups.Count; i++)
{
Text += match.Groups[0].Value.ToString() + " ";
}
match = match.NextMatch();
}
s = "123456789a123456789"; :
123 456 789
2
s = "123456789a123456789"; :
123 456 789
(?=...)
(?!...)
:
? ,
,
;
=
,
,
.
1:
private void button3_Click(object sender, EventArgs e)
{
StringBuilder mystringbuilder = new StringBuilder();
mystringbuilder.Append("<html><head><title>");
mystringbuilder.Append(" ");
mystringbuilder.Append("
.NET</TITLE></head><body>");
mystringbuilder.Append("<p>
\n</p>");
mystringbuilder.Append("<p> \n");
mystringbuilder.Append(" </p>");
mystringbuilder.Append("<p>
:
? ,
,
;
=
,
.
:
? ,
,
(?<=...)
;
<=
,
,
.
</p>");
mystringbuilder.Append("</body></html>");
Regex
regex
=
new
Regex(@"(?<=<title>).*(?=</title>)",
RegexOptions.IgnoreCase | RegexOptions.Singleline
| RegexOptions.ExplicitCapture);
try
{
MatchCollection matchcollection =
regex.Matches(mystringbuilder.ToString());
//matchcollection = regex.Matches(s);
Text = ""; textBox1.Text ="";
for (int i = 0; i < matchcollection.Count; i++)
{
Text += matchcollection[i].Value;
}
}
catch (Exception ex)
{
Text=ex.Message;
}
}
:
.NET
2.
P:
(?<!...)
:
? ,
,
;
!
,
.
.... HTML
........
Regex regex = new Regex
(@"(?<=<p>)(?<name1>[^(<p>)]*)(?=</p>)",
RegexOptions.IgnoreCase
|
RegexOptions.ExplicitCapture
| RegexOptions.Singleline);
try
{
MatchCollection matchcollection =
regex.Matches(mystringbuilder.ToString());
for(int i = 0; i < matchcollection.Count; i++)
{
Text += matchcollection[i].Result("${name1}") + " ";
}
}
catch (Exception ex)
{
textBox1.Text=ex.Message;
}
}
( ):
: \t - ,
\r - , \n - , \v - , \e - Escape, \f , \0AA - (AA)
, \xAA - (AA) ,
\xAAAA- (AAAA) ,
\a - .
3. :
( )(?:...)
?, +, *, {m,n}
abc,\, \Z
4.
button2_Click, 2.
string s = ": wladm@narod.ru : http://wladm.narod.ru ";
Regex regex = new Regex(@"\b\w+([\.\w]+)*\w@\w((\.\w)*\w+)*\.\w{2,3}\b");
\b , \w+ (
) 1 . ([\.\w]+)*\w@
@,
. ( ) 1
0 . \w@
\w, @ . .
, @ -
(ru, com....).
2 , - MSDN
Match. ,
.
:
\b\w+:\/\/\
http://
: (\/\w*|\.\w*|\?\w*\=\w*)*) -
("/",".","?","=") "|".
:
http://msdn.microsoft.com/.html
http://msdn.microsoft.com/library/rus/default.asp?url=
/library/RUS/cpref/html/frlrfSystemTextRegularExpressionsMatchClassTopic.asp
:
string s = " = +225 : 211 : 335 2222 -357.8 ";
Regex regex = new Regex(@"[-+]?\d*\.?\d*");
[-+]? - 0 1 , \. 0 1
, 0 .
:
225
211
335
2222
-357.8
string
s = " - : , ?
Regex regex = new Regex(@"\b(\w+)\,?\??\!?(\ \-\ )?\:?\ ?");
.......
// ,
// ( \ ?).
Text += matchcollection[i].Value;
";
:
- : , ?
string s = ": 812-555-55-55 : 555-55-55 : 848-222-22-22";
Regex regex = new Regex
(@"\b(\d{3}){0,1}(\-){0,1}(\d{3}\-)(\d{2}\-)(\d{2})\b");
. ,
. :
812-555-55-55 555-55-55 848-222-22-22
,
() :
string s = ": (812)5555555 : 555-55-55 : (848)222-22-22";
Regex regex = new Regex
(@"((\(\b\d{3}\))|(\b(\d{3}){0,1}(\-){0,1}))(\d{7}|((\d{3}\-)(\d{2}\-)(\d{2})\b))");
, ,
, , ,
.
:
(812)5555555 555-55-55 (848)222-22-22
,
,
. ,
.
1111111 412.86
2222222 626.63
3333333
4444444 732.96
5555555 288.23
288.67
135.00
285.34
156.74
701.53
626.63
54.40
1072.70
579.97
,
. .
" " ,
. tab, . "" tab , .
\t1111111\t412.86\t\t288.67\t\t\t701.53
\t2222222\t626.63\t\t\t\t\t626.63
\t3333333\t\t\t\t\t\t
\t4444444\t732.96\t\t285.34\t54.40\t1072.70
\t5555555\t288.23\t135.00\t156.74\t\t\t579.97
:
1 - :
string sS= ;
Regex r = new Regex(@"\d{7}", RegexOptions.IgnoreCase);
Match mymatch = r.Match(sS);
if(mymatch.Success)
{
string sTel = mymatch.Groups[0].Value.ToString();
...
2 - , :
:
MatchCollection matchcollection = r3.Matches(sS);
5.
(+ *) .. - ..
, .
string s =
" , ?";
Regex regex = new Regex(@"\b(.*)o");
//
..Regex regex = new Regex(@"\b(.+)o");
:
,
6. Regex
6.1.
Regex ,
options - OR RegexOption.
public Regex
(
string pattern,
RegexOptions options
);
, "" RegexOptions .
Compiled - ,
, . ,
, Regex
.
CultureInvariant - .
ECMAScript - ,
ECMAScript. IgnoreCase, Multiline
Compiled, .
ExplicitCapture - ,
( (?)) .
(?::).
IgnoreCase - .
IgnorePatternWhitespace -
, "#".
Multiline - ^ $ .
RightToLeft - .
SingleLine - .
\n.
- :
Match - . Match,
.
Matches -
MatchCollection.
NextMatch - Match
.
. .
6.2. Regex
,
Replace. 10
, :
input - .
pattern - .
replacement -, .
evaluator - MatchEvaluator .
options - OR RegexOption
(. ).
count - .
startat - , .
c ,
,
.
:
1. HTML Title:
StringBuilder mystringbuilder = new StringBuilder();
mystringbuilder.Append("<html><head><title>");
mystringbuilder.Append(" ");
mystringbuilder.Append(" .NET</TITLE></head>");
mystringbuilder.Append("<body><p> </p>");
mystringbuilder.Append("</body></html>");
Regex regex = new Regex(@"(?<=<title>).*(?=</title>)",
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture
| RegexOptions.Singleline);
string s = regex.Replace(mystringbuilder.ToString(),
" Net");
textBox1.Text = s;
Net
2. :
private int viI=0;
private void button4_Click(object sender, EventArgs e)
{
string s = "abcdeabrtfabqwe";
Regex regex = new Regex("ab");
// TextBox
Text = ""; textBox1.Text="";
//Replace myMatchReplace
string d = regex.Replace(s,myMatchReplace);
textBox1.Text = d;
}
public string myMatchReplace(Match match)
{
if(match.Success)
{
for (int i = 0; i < match.Groups.Count; i++)
{
Text += ": "+Convert.ToString(viI)+" "
+match.Groups[0].Value.ToString() + " ";
}
}
viI++;
return ": "+Convert.ToString(viI-1)+" ";
}
Text ,
:
: 0 ab : 1 ab : 2 ab
TextBox , , regex.Replace(s,MatchReplace).
, ,
:
: 0 cde : 1 rtf : 3 qwe
6.3.
IsMatch: 2
, :
input - .
startat - , .
true, false
.
:
string s = "abcdeabrtfabqwe";
Regex regex = new Regex("ab");
bool f = regex.IsMatch(s);
if(f)
Text = " ";
else
Text = " ";
6.4.
Split: 2
, :
input - .
startat - , .
count - .
, ,
, . count,
count ( , ).
Count=1 , 2 - 2 -
..
:
string s = "cdabcdeabrtfabqwe";
Regex regex = new Regex("ab");
string[] s1 = regex.Split(s,2);
for (int i = 0; i < s1.Length; i++)
{
textBox1.Text += s1[i] + " ";
}
:
cd cdeabrtfabqwe
7.
.
.
7.1 Match Captures
, Match -
. Match Captures,
Group. Captures
. , ,
, ,
CaptureCollection ( Capture) Capture.
(,
- {1,2}):
private void button4_Click(object sender, EventArgs e)
{
string s = "1 2 3 4 5 ";
string sregx = @"((\d+)\s+()\s+){1,2}";
Regex regex = new Regex(sregx, RegexOptions.IgnoreCase);
Match match = regex.Match(s);
int viI0 = 1;
while (match.Success)
{
System.Console.WriteLine
(" " +
Convert.ToString(viI0)+" ");
viI0++;
System.Console.WriteLine("Match : " + match.ToString());
while(match.Success)
{
System.Console.WriteLine
(" " +
Convert.ToString(viI0) + " ");
viI0++;
System.Console.WriteLine("Match : " + match.ToString());
for(int i = 0; i < match.Groups.Count; i++)
{
Group group = match.Groups[i];
System.Console.WriteLine("Group "+Convert.ToString(i)+
" : " + group);
}
match = match.NextMatch();
}
}
Output Form ( View\Output):
1
Match : 1 2
Group 0 : 1 2
Group 1 : 2
Group 2 : 2
Group 3 :
2
Match : 3 4
Group 0 : 3 4
Group 1 : 4
Group 2 : 4
Group 3 :
3
Match : 5
Group 0 : 5
Group 1 : 5
Group 2 : 5
Group 3 :
7.3. Match, Group, Capture,
,
- , Group Capture.
private void button1_Click(object sender, EventArgs e)
{
string s = "1 2 3 4 5 ";
string sregx = @"((\d+)\s+()\s+){1,2}";
Regex regex = new Regex(sregx, RegexOptions.IgnoreCase);
Match match = regex.Match(s);
int viI0 = 1;
while(match.Success)
{
System.Console.WriteLine
(" " +
Convert.ToString(viI0) + " ");
viI0++;
System.Console.WriteLine("Match : " + match.ToString());
for(int i = 0; i < match.Groups.Count; i++)
{
Group group = match.Groups[i];
System.Console.WriteLine("Group "+Convert.ToString(i)+
" : " + group);
int viI = 0;
foreach (Capture capture in group.Captures)
{
System.Console.WriteLine("Capture " + Convert.ToString(viI)
+" : "+capture);
viI++;
}
}
match = match.NextMatch();
}
}
Output Form ( View\Output):
1
Match : 1 2
Group 0 : 1 2
Capture 0 : 1 2
Group 1 : 2
Capture 0 : 1
Capture 1 : 2
Group 2 : 2
Capture 0 : 1
Capture 1 : 2
Group 3 :
Capture 0 :
Capture 1 :
2
Match : 3 4
Group 0 : 3 4
Capture 0 : 3 4
Group 1 : 4
Capture 0 : 3
Capture 1 : 4
Group 2 : 4
Capture 0 : 3
Capture 1 : 4
Group 3 :
Capture 0 :
Capture 1 :
3
Match : 5
Group 0 : 5
Capture 0 : 5
Group 1 : 5
Capture 0 : 5
Group 2 : 5
Capture 0 : 5
Group 3 :
Capture 0 :
: Match Groups
GroupCollection , ,
Group:
MSDN. System.Text.RegularExpressions -
VB .Net
(.net, c#)
21.09.2006.
c -
Veles -
, - ,
, Bricks -
, , TellMe - - ,
,
Borland C++ Builder, C# (Windows ASP.Net Web ).