Regular Expressions in .NET - PowerPoint PPT Presentation

About This Presentation
Title:

Regular Expressions in .NET

Description:

System.Text.RegularExpressions Regex. ... Perl 5 ... – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 67
Provided by: Svetli6
Category:

less

Transcript and Presenter's Notes

Title: Regular Expressions in .NET


1
(No Transcript)
2
????????? ??????
???????????? ?? .NET Framework
http//www.nakov.com/dotnet/
??????? ?????
?????????? ???????? ?? ?????????? ?? ???????
academy.devbg.org
3
?????????? ??????
  • ?????? ???????? ?? ?????? ??????? ?? ?????? ?
    .NET (Common Type System)
  • ?????? ???????? ?? ????? C
  • ???????? ?? ?????????? ?? ?????? ??? ????????
    ?????? ? .NET Framework

4
??????????
  • ????????? ??????. ????????? ?? ??????????? ??????
  • ?????? ?? ??????????? ??????
  • ???????? ? ???????????. Escaping
    ?????????????????, ??????????? ?? ???????,
    ??????????? ?? ??????????, ??????????? ??
    ??????????????
  • ????????? ?????? ? .NET. ??????????????
    System.Text.RegularExpressions
  • ?????? Regex. ??????. ??????????
  • ????????? Match ? MatchCollection
  • ??????? ?? ????????? ?????

5
?????????? (2)
  • ????????? ?? ????? ?? ?????????? ???? ????? ?
    ??????????? ??????
  • ?????????? ? ????? ? ????????? ?????
  • ????????? ?? ??? ?? ????????? ?????
  • ????????? ? RegexOptions
  • Escaping ????????
  • ???? ?? ???????? ????????? ???????
  • ?? ????????????? ?? ??????????? ??????
  • ????????? ?????? ??? ???????
  • ?????? ????????? ??????. ???????????? "The
    Regulator"

6
????????? ??????
  • ??????????? ?????? ?? ?????, ?????? ? ?????????
    ???????? ?? ????????? ?? ?????
  • ? ????????? ?????? ?????
  • ?? ?????? ? ????????? ?????????? ?? ????? ?????
    ?? ????? ??????
  • ?? ?????????? ???????? ??????????
  • ?? ???????? ? ????????? ????????? ? ????? ?????
    ???? ???????
  • ??????????? ?????? ????????? ???? ?????????????
    ?????? ???????? ?? ???????? ???????? ?
    ??????????? ?????
  • ? .NET Framework ??????????? ?????? ????
    ?????????? ?? Perl 5

7
????? ? ????????? ??????
  • ????????? ????? ? ???????? ???, ????? ??????
    ??????? ?????????? ?? ???????? ?????? (?????????
    ????)
  • ??????????? ?????? ????????? ????????? ?????????
    ?? ???????? ?? ????????
  • ???????? ??????????? ?????
  • ?????????? ?????? ???????? ??????, ????? ??
    ??????? ???? ?? ??????? 0 ? 1, ? ?????
  • ?????????? ?????? ????????? ??????, ????? ????
    ???? 088XXXXXXX (X ? ?????)

0-1
0880-97
8
????????? ?????? ? .NET ??????
static void Main(string args) string text
"???? ?????, ???? ????, ???? ?'?? ??
??." // ????????? ????? ?? ??????? ??
????????????????? // ?? ????? ? ?????,
?????????? ?? "???", ?????? // "?????" ?
????, ????????? ? ???? ?? ?????? "??" string
pattern _at_"\w????????\w??\w" Match
match Regex.Match(text, pattern) while
(match.Success) Console.WriteLine(
"??? \"0\" - ?????? 1, ???????
2", match, match.Index,
match.Length) match match.NextMatch()

9
???????????? 1
  • ??????? ?? ?????????? ? ????? ? ????????? ?????

10
????????? ? ????????? ??????
  • ??????????? ?????? ?? ????? ?????? ?? ?????????
    ?? ?????? ?????, ???????? ?? ???????? ?? email
    ?????
  • ??????? email ??????
  • svirka_at_kaval.com, sladurana_at_duduk.net,
    -123--_at_usa.net, test.test123_at_en.some-host.12345.co
    m
  • ????????? email ??????
  • .ala._at_bala.com, test_at_-host.com, user_at_.test.ru,
    user_at_test.ru., alabala_at_, user_at_host, _at_eu.net

string email "test-1.p46_at_ala-bala.somehost.somew
here.bg" string regex _at_"(a-zA-Z0-9_\-a-zA-Z
0-9_\-\.0,49)" _at_"_at_((a-zA-Z0-9a-zA-Z0-9\
-0,49\.)a-zA-Z2,4)" bool valid
Regex.IsMatch(email, regex) Console.WriteLine(val
id)
11
?????? ?? ??????????? ??????
  • ??????????? ?????? ?? ???????? ?? ???????? ??
    ?????????, ????? ?????? ? ????? ?????
  • ??????????? ?? ??????????? ?????? ?? ?????? ??
    ???????? ? ???????????
  • ?????????? ?? ????? ?? ??????, ????? ??????
  • ????????????? ?? ???????, ????? ??????? ?????????
    ???????? ? ??????? ?? ???????
  • ????????, ? ?????????? ?????
  • ???????? ?? "???", "?????" ? "??", ? ???????????
    ?? "\w", "" ? ""

\w????????\w??\w
12
???????????
  • ????????????? ? ??????????? ?????? ?? ???????
    ?????????
  • Escaping ?????????????????, ???????? \
  • ??????? ?? ???????, ???????? a-zA-Z
  • ??????????? ?? ???????? ?? ????????????
    (quantifiers), ???????? ?
  • ??????????? ?? ???????? ?? ???????????????? ?
    ??????, ???????? \b
  • ??????????? ?? ???????????? ?????, ????????
    (????????? "???")
  • ????????? ???????????, ???????? (0-9)
  • ????? ???????????, ???????? (?? ???????? ??
    ?????????), ?????????? (???????? 1),

13
Escaping ?????????????????
  • \t ?????????
  • \r ?????? ?? ??????? ?? ????????? CR (0x0D)
  • \n ?????? ?? ??? ??? LF (0x0?)
  • \xXX ?????? ? ASCII ??? XX (??????????????)
  • \uXXXX Unicode ?????? ? ????? XXXX
    (??????????????)
  • \\ ?????? \
  • \ ??????
  • \ ??????

14
??????? ?? ???????
  • . ?????????? ?????????? ?????? ??? \n
  • (.\s) ?????????? ?????????? ??????
  • ??????? ?????????? ?????????? ?????? ??
    ??????????
  • ?????? 01 ?????????? ??????? 0 ??? 1
  • ??????? ?????????? ?????????? ??????, ?????
    ?? ? ???? ??????????
  • ?????? ltgt\\ ?????????? ????? ?????? ??? lt, gt
    ? \
  • charX-charY ?????????? ?????? ? ?????????
    ????????
  • ?????? 0-9A-F ?????????? ????? ??????, ????? ?
    ????? ??? ???????? ????? ????? A ? F

15
??????? ?? ???????
  • \w ?????????? ???????, ??????? ? ??????? _ (??
    ?????? ????? ?? Unicode)
  • \W ?????????? ?????? ??????? ? ?????????? ??
    ???????, ??????? ? _
  • \s ?????????? ????????? ?? ?????? ????????????
    (????????, ?????????, ??? ???, ...)
  • \S ?????????? ?????????, ????? ?? ?? ??????
    ????????????
  • \d ?????????? ??????????? ????? 0-9
  • \D ?????????? ?????? ???????, ????? ?? ??
    ????????? ?????

16
??????????? ?? ??????????
  • ???? ??? ?????? ????????
  • ?????? 01 ?????????? ?????? ???????? ??????,
    ????????? ?? ??????? 0 ??? 1, ??????????? ???????
    ???
  • ???? ??? ?????? ????????
  • ?????? A-Z ?????? ?????????? ???????? ??????,
    ????????? ?? ?????? ???????? ?????
  • ? ???? ??? ???? ????????
  • ?????? A-Z? ?????????? ?????? ???????? ?????
    ??? ?????? ???
  • n ????? n ????????
  • ?????? 0-93 ?????????? ???????????????? ??
    ????? 3 ?????

17
??????????? ?? ??????????
  • n, ???? n ????????
  • ?????? 0-95, ?????????? ???????????????? ??
    ???? 5 ?????
  • n,m ???? n ? ???-????? m ????????
  • ?????? 0-92,4 ?????????? ???????????????? ??
    2, 3 ??? 4 ?????
  • ? ???? ??? ?????? ????????, ?? ???-???????
    ???????? ????
  • ? ???? ??? ?????? ????????, ?? ???-???????
    ???????? ????
  • n,? ???? n ????????, ?? ???-??????? ????????
    ????

18
??????????? ?? ??????????????
  • \b ???????? ?? ??????? ???? ? ???????? ??? ????
    ?? ???? ?? ????????? ????? ????????? \w ? \W,
    ?? ? ??????? ?? \w
  • ?????? \b\w???\w\b ?????????? ?????? ????,
    ????????? ???? ?????? "???", ???????? "????",
    "??????" ?? ?? ? "????? ?? ???????"
  • ?????? .???\b ?????????? ?????? ???-????
    ?????????, ?????????? ?? "??", ???????? "????
    ????" ? "??? ????", ?? ?? ? "???? ????" ? "?????
    ?? ??????"
  • \B ???????? ?? ??????? ???? ? ??????? ?? ??????
    (??? ???????? ? ????)
  • ?????? \w????\B ?? ?????? "????" ? ?????? "????
    ?? ? ???????", ?? ???? ?? ?????? ???? ? ??????
    "????? ?? ????, ???? ??!"

19
??????????? ?? ??????????????
  • \A ???????? ?? ??????? ???? ? ???????? ??
    ????????? ????? (?????? ?? ????? ????)
  • ?????? \A???? ?? ?????? "????" ? ?????? "??????
    ? ??????", ?? ???? ?? ?????? ???? ? ?????? "?????
    ??? ???? ?? ? ?????!"
  • \z ???????? ?? ??????? ???? ? ???? ?? ??????
    (?????? ?? ???? ????)
  • ?????? ????\Z ?? ?????? "????" ? ?????? "?????
    ????", ?? ???? ?? ?????? ???? ? ?????? "?????? ?
    ??????"
  • ??????? ???? ? ???????? ?? ?????? (? ?????
    multi-line ? ? ???????? ?? ????? ???)
  • ??????? ???? ? ???? ?? ?????? (? ?????
    multi-line ? ? ???? ?? ????? ???)

20
????? ???????????
  • ??????????? ?? ?????
  • AB ?????? ???????????? ????? ????? ???????????
    ?????? A ? B
  • ?????? ????????? ?? ?????? "????" ? ??????
    "??????? ?? ? ???????", ? ? ?????? "???? ?
    ????????" ?? ?????? "?????"
  • ??????????? ?? ???????? ?? ?????
  • (?????), (?lt???gt?????) ?????? ????? ?
    ?????????? ????? (??? ??? ??? ? ???)
  • ?????? \s(?ltnamegt\w)\s\s(\d)
  • ??????? ?? ????????? ?? ????????? ???????? ??
    ????? ?? ?????????? ????? ? ????? ?? ???? ?????

21
??????????? ?????? ? .NET
  • ? .NET Framework ?????????? ?? ????????? ?? ?????
    ???? ????????? ?????? ?? ??????????? ?
    ?????????????? ?? ????? System.Text.RegularExpress
    ions
  • ?????? Regex ??????? ????????? ????????? ????? ?
    ?????? ?? ???????, ?????????? ? ????????? ??
    ?????? ???? ???? ?????
  • Regex ?????????? ? ???????? ?????? ?? ??????
    ??????? ????????
  • ?????? Match ??????? ???????? ?? ???? ??????????
    (????????, ??????? ??????? ? ???????), ???????? ?
    ???????? ?? ??????? ? ????????? ?????
  • ???? ?????????? ?? ???????? ?? ??????????
    ?????????? ?? ?????????, ??? ?????? ???

22
??????????? ?????? ? .NET
  • ????????? ?? ?????????????? ? ?????
    System.Text.RegularExpressions
  • ?????? MatchCollection ??????? ?????? ??
    ?????????? (???????? ? ???????? ?? ???????)
  • ?????? Group ???????????? ????? ?? ???????,
    ????????? ?? ? ?????? ?????????? (Match). ? ????
    ?????????? ???? ?? ??? ??????? ?????
  • ?????? GroupCollection ??????? ?????? ?? ?????,
    ????????? ?? ? ?????? ??????????
  • ????????? MatchEvaluator ?? ???????? ???
    ?????????? ? ????????? ????? ?? ????????? ??
    ????? ???? ??????????
  • ?????????? ??? RegexOptions ?? ???????? ??
    ???????? ?? ????? ?? ????????? ? ???. ??????

23
?????? Regex
  • ? .NET Framework ?????? Regex ? ???-??????? ????
    ?? ?????? ? ????????? ??????
  • ? Regex ???? ?? ?? ?????? ?? ??? ??????
  • ?????????? ?? ?????? ? ? ???????????? ?? ??
    ?????? ????????? ?????, ???? ????? ?? ?? ????????
    ???????? ?? ????????? ?? ????? (IsMatch, Match,
    Matches, Replace, Split)
  • ????????? ?? ?????????? ?????? ?? ????? (IsMatch,
    Match, Matches, Replace, Split), ?? ????? ??
    ?????? ?????? ?? ????????? ? ????????? ?????,
    ????? ?? ?? ????????
  • ???????? ? ?????????????? ?? Regex ? ??-?????????
    ??? ? ???? ? ??? ????? ?? ?? ??????????
    ?????????????? ??????? ??????

24
?????? Regex
  • ??-????? ?????? ? ???????? ?? Regex
  • IsMatch(text, pattern) ????????? ???? ? ?????
    ????? ?? ????? ???? ???? ??????, ?????
    ??????????? ?? ????? ????????? ?????
  • Match(text, pattern) ????? ????????? ?????????
    ????? ? ????????? ????? ? ????? ???????
    ?????????? ???? Match ?????
  • Matches(text, pattern) ????? ?????????
    ????????? ????? ? ????????? ????? ? ?????
    MatchCollection ?? ?????? ??????????
  • Replace(text, pattern, replacement) ????????
    ?????? ???????? ?? ????? ????????? ????? ? ?????
    ????? ??? ????????? ?????, ????? ???? ?? ???????
    ????? ?? ?????????? ?????????? (????? ? ???.
    ?????)

25
?????? Regex
  • ??-????? ?????? ? ???????? ?? Regex
  • string Split(text, pattern) ??????? ????? ???
    ?? ????? ?? ????? ????????? ?????
  • Escape(text) ???????? ?????? ????????? ???????
    ?? ????? ?? ??????????? ?????? ? ????????????
    escaping ?????????????????
  • Unescape(text) ????????? ?? Escape()
  • GetGroupNames() ????? ????? ?? ??????? ??
    ???????, ?????????? ? ????? ????????? ?????
    (??????? ??? ????? ??????????? ????????? ????????
    ???)
  • Options ???????? ?? ??? RegexOptions, ?????
    ?????? ????? ????????? ?? ?????? (????
    Singleline, Multiline, IgnoreCase ?
    IgnorePatternWhitespace)

26
Regex.IsMatch ??????
static bool IsPositiveInteger(string aNumber)
// ?????? ??????????? ????? ???????? ? ????? ??
1 ?? 9 ?? // ????? ??????? ? ????????? ? 0
??? ?????? ????? ? ???? Regex numberRegex
new Regex(_at_"\A1-90-9\Z") return
numberRegex.IsMatch(aNumber) static void
Check(string aText) Console.WriteLine("0
- 1", aText, IsPositiveInteger(aText) ?
"positive integer" "NOT a positive
integer") static void Main(string args)
Check("123456") // 123456 positive integer
Check("15 16") // 15 16 NOT a positive
integer
27
Regex.Match ??????
static void Main(string args) string text
_at_"lthtmlgt This is a hyperlink lta
href""javascript'window.close()'""gt close
the windowlt/agtltbrgt ... and one more link lta
target""_blank"" href/main.aspx class'link'gt
ltbgt main pagelt/bgt lt/agtlt a href
'http//www.nakov.com' gt ltimg
src'logo.gif'gtNakov's home site lt /a gt"
string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt" Match match
Regex.Match(text, hrefPattern) while
(match.Success) Console.WriteLine("
0\n\n", match) match
match.NextMatch()
28
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ??????? ??? ??????? "lt" ? ????????? ???? ????????
    ???????????? ???? ???? (??? ???)

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
29
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ??????? ??? ??????? "lt" ? ????????? ???? ????????
    ???????????? ???? ???? (??? ???)
  • ????? ?????? "a", ??????? ???????????? ?? ??????
    ????????????

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
30
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ??????? ??? ??????? "lt" ? ????????? ???? ????????
    ???????????? ???? ???? (??? ???)
  • ????? ?????? "a", ??????? ???????????? ?? ??????
    ????????????
  • ????????? ???? ??????????? ???? ??????? ??????
    ?????? ???? "href" (??? ????? ??? ????? ????????
    ????? "href", ?? ????????)

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
31
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ??????? ??? ??????? "lt" ? ????????? ???? ????????
    ???????????? ???? ???? (??? ???)
  • ????? ?????? "a", ??????? ???????????? ?? ??????
    ????????????
  • ????????? ???? ??????????? ???? ??????? ??????
    ?????? ???? "href" (??? ????? ??? ????? ????????
    ????? "href", ?? ????????)
  • ????? ??????? "", ?????????? ??????????? ?
    ??????? ?? ?????? ????????????

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
32
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ??? ??????? ?????? ??????? ??? ????????,
    ????????? ???? 0 ??? ?????? ??????? ?? ????????
    ?? ????????? ????????? ?????? ??????? ??? ????????

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
33
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ??? ??????? ?????? ??????? ??? ????????,
    ????????? ???? 0 ??? ?????? ??????? ?? ????????
    ?? ????????? ????????? ?????? ??????? ???
    ????????
  • ??? ?? ??????? ?????? ??????? ??? ????????,
    ????????? ???? 0 ??? ?????? ??????? ???????? ??
    ?????? ????????????

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
34
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ??? ??????? ?????? ??????? ??? ????????,
    ????????? ???? 0 ??? ?????? ??????? ?? ????????
    ?? ????????? ????????? ?????? ??????? ???
    ????????
  • ??? ?? ??????? ?????? ??????? ??? ????????,
    ????????? ???? 0 ??? ?????? ??????? ???????? ??
    ?????? ????????????
  • ???????? ?????? ??????? ?? ???????? ?? ?????? "gt"
    ? ????????? ???? ????

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
35
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ????????? ???? 0 ??? ?????? ?????????? ???????,
    ???? ?? ?????? ????? ?? ?? ? ????????? (?????????
    ???? ? ????? ?????)

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
36
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ????????? ???? 0 ??? ?????? ?????????? ???????,
    ???? ?? ?????? ????? ?? ?? ? ????????? (?????????
    ???? ? ????? ?????)
  • ????? ???????? ??? "lt/agt", ?????????? ???????? ??
    ????? ????????? ??????? ?????? ????????????
    (whitespace)

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
37
??? ?????? ?????????
  • ??????????? ?????, ????????? ? ???????
  • ????? ?????? ????
  • ????????? ???? 0 ??? ?????? ?????????? ???????,
    ???? ?? ?????? ????? ?? ?? ? ????????? (?????????
    ???? ? ????? ?????)
  • ????? ???????? ??? "lt/agt", ?????????? ???????? ??
    ????? ????????? ??????? ?????? ????????????
    (whitespace)
  • ?????? ?????????? ?? ??????? ?????????????? ????
    Match.NextMatch()

string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('''""""""\S)gtgt"
_at_"(.\s)?lt\s/a\sgt"
38
Regex.Matches ??????
static void Main() // ????????? ????? ??
??????? ?? ???? ?? ???????? Regex regex new
Regex(_at_"\b?-??-?\b") String text
"The Bulgarian word '????' (beer) often"
" comes with the word '?????' (grill)."
MatchCollection matches regex.Matches(text)
foreach (Match match in matches)
Console.Write("01 ", match) //
???????? ???? ?????
39
?????? Match
  • ??-????? ???????? ? ?????? ?? Match
  • Success ????? ???? ???????????? ? ???????
    ???? ????????? ? ???????? ????
  • Value ????? ?????????? ?? ????????????
  • Index ????? ????????? ?? ???????????? ? ??????
  • Length ????? ????????? ?? ????????????
  • NextMatch() ??????????? ???????????? ??
    ????????? ?? ???? ?? ???????? ?????????? ? ?????
    ?????????? ?????????? (??? ???)
  • Groups ????? ???????, ????????? ?? ?
    ???????????? ??? ??? ?? GroupCollection
  • Captures ????? CaptureCollection ?? Capture
    ????????, ?????????? ????????????

40
Regex.Match ??????
static void Main() // ????????? ????? ??
??????? ?? ???? ?? ???????? Regex regex new
Regex(_at_"\bA-Za-z\b") String text
"???????? ?????????? ??????, ?? ?????????? "
"???? (Humulus lupulus) ?????? ?????? ?
" "????????????? ???? ???? IX ???."
Match match regex.Match(text) while
(match.Success) Console.Write("0
1 ", match, match.Index) match
match.NextMatch() // ????????
Humulus47 lupulus55 IX103
41
?????? ? ?????
  • ??????? ? ??????????? ?????? ????? ?????????? ??
    ????????? ? ????????? ?? ??????? ????? ??
    ????????
  • ??????? ?? ??????? ??? ????? ? ????? ?? ????
    ?????
  • ??????? ????? ?? ?? ?????? ???? ? ?????
  • ?? ????????? ?? ?????? ????? ?? ?????? ??????????
    (Match) ?? ???????? ?????????? Groups, ?????
    ????? GroupCollection
  • ???????, ?? ????? ?? ?? ???????? ?????, ????? ??
    ?? ???????? ?? ?????
  • ??????????? ????? ?? ???? ?? ?????????? ?????

42
?????? ? ????? ??????
  • ????? ? ????? ??? ??????
  • ???????? ? ????????? ?????? ? ?????

lt??? ?? ??????????gt ltIP ?????gt lt????? ? ?????????gt
String text "gosho 62.44.18.124 024450\n"
"root 193.168.22.18 221238\n" "nakov
217.9.231.126 000724" string pattern
_at_"(?ltnamegt\S)\s(?ltipgt0-9\.)\s(?lttimegt0-9
)" MatchCollection matches Regex.Matches(text,
pattern) foreach (Match match in matches)
Console.WriteLine("name0,-8 ip1,-16
time2", match.Groups"name",
match.Groups"ip", match.Groups"time")

43
?????? ? ????? ??????
  • ????? HTML ???????? ? ?????? ?? ???????? ?? ????
    ?????? ??????????? ???? ??????? ????????? ??
    ????????

static void Main(string args) string text
_at_"lthtmlgt This is a hyperlink lta
href""javascript'window.close()'""gt
close the windowlt/agtltbrgt ... and one more link
lta target""_blank"" href/main.aspx
class'link'gt ltbgt main pagelt/bgt lt/agtlt a
href 'http//www.nakov.com' gt ltimg
src'logo.gif'gtNakov's home site lt /a gt"
string hrefPattern _at_"lt\sa\sgt\bhref\s\s"
_at_"('(?lturlgt')'""(?lturlgt"")"""
_at_"(?lturlgt\S))gtgt"
_at_"(?ltlinktextgt(.\s)?)lt\s/a\sgt" (????????
??????????)
44
?????? ? ????? ??????
Match match Regex.Match(text,
hrefPattern) while (match.Success)
string hyperlink match.Value
hyperlink Regex.Replace(hyperlink, _at_"\s", "
") Console.WriteLine("hyperlink0",
hyperlink) string linktext
match.Groups"linktext".Value linktext
Regex.Replace(linktext, _at_"\s", " ")
linktext linktext.Trim()
Console.WriteLine("linktext0", linktext)
string url match.Groups"url".Value
Console.WriteLine("url0", url)
Console.WriteLine() match
match.NextMatch()
45
???????????? 2
  • ????????? ?? ?????? ?????? ?? HTML ???????? ????
    ????????? ??????

46
??????? ??????????
  • ????????? ?????????? (back references) ??????????
    ????????? ?? ???????? ?????????? ?? ?????
  • ?????? ???????? ?? ?????? ?????????? ??
    ??????????? ??? ??????????? ?????? ? ????? ???
    ?????? "?????????? ??????"

String text "gosho boza!!36\n" "pesho
pesho\n" "ivo kaka\n" "kaka k_at_k_at_22\n"
"test test" string pattern _at_"(?ltusergt\S)\s(\
ltusergt)" MatchCollection matches
Regex.Matches(text, pattern,
RegexOptions.Multiline) foreach (Match match in
matches) Console.Write("0 ",
match.Groups"user") // ???????? pesho test
47
Regex.Replace ??????
  • ????? ???????? ? ?????? URL /URL. ??????
    ?? ?? ???????? ? HTML ??????????? lta href gt
    lt/agt

static void Main() String text "Here is
the linkltbrgt" "URLhttp//www.devbg.org
????/URLltbrgt\n" "and the
logoURLhttp//www.devbg.orgIMG\n"
"http//www.devbg.org/basd-logo.png/IMG/URL\n"
string pattern _at_"\URL(?lturlgt\)\"
_at_"(?ltcontentgt(.\s)?)\/URL\"
string newPatt "lta href\"url\"gtcontentlt/a
gt" string newText
Regex.Replace(text, pattern, newPatt)
Console.WriteLine(newText)
48
Regex.Replace ??????
  • Regex.Replace ???? ?? ?? ???????? ? ? ????????
    MatchEvaluator, ????? ?? ??????? ?? ?????????? ??
    ?????????? ??????????

static string CapitalizeFirstLetter(Match
aMatch) string word aMatch.Value
return Char.ToUpper(word0) word.Substring(1)
static void Main() String text "??????
????? - ???? ????????!" string pattern
_at_"\w" string newText Regex.Replace(text,
pattern, new MatchEvaluator(CapitalizeFir
stLetter)) Console.WriteLine(newText)
// ???????? ?????? ????? - ???? ????????!
49
Regex.Split ??????
  • ????????? ?? email ?????? ?? ?????? ?
    ???-???????????? ???????????
  • ??? ?????????? ????? ? ?????????? ?????????
    ?????, ?? ???????? ? ?????????

String text "zuzi_at_kaval.bg ciki_at_duduk.net, "
"bob_at_mail.bg\n\nfn12345_at_fmi.uni-sofia.bg\n"
" mente_at_eu.int , ,
gero_at_dir.bg" string splitPattern
_at_",\s\" string emails
Regex.Split(text, splitPattern) Console.WriteLine
(String.Join(", ", emails)) // ????????
zuzi_at_kaval.bg, ciki_at_duduk.net, bob_at_mail.bg, //
fn12345_at_fmi.uni-sofia.bg, mente_at_eu.int,
gero_at_dir.bg
string parts Regex.Split("?????-????",
"(-)") // parts "?????", "-", "????")
50
????????? ? RegexOptions
  • ?????? Regex ???? ?? ?????? ???????? ?????????
    ???? ?????????? ?? Options
  • ?????????? ????????? ?? ??????? ? ????????? ???
    RegexOptions
  • IgnoreCase ?????? ???????, ????? ????????
    ????????? ?? ??????? (?????/??????)
  • Multiline ?????? ????? ?? ??????? "multi-line".
    ????????????? ? ? ???? ????? ????????? ??????
    ? ???? ?? ???
  • Singleline ?????? ????? ?? ???????
    "single-line". ???????????? . ? ???? ????? ???
    ???????? "????? ??????, ??????????? \n"
  • IgnorePatternWhitespace ???????? ????????
    ???????????? ? ??????????? ( ) ? ???. ?????

51
RegexOptions ???????
String text "?????? ??????. ????? ???
????!" string pattern _at_"\b???\w\b" MatchColle
ction matches Regex.Matches( text, pattern,
RegexOptions.IgnoreCase) foreach (Match match in
matches) Console.Write("0 ", match) //
?????? ??????? IgnoreCase ?????????? ? ??????
????
String text "?????? ??????.\n????? ???
????!" string pattern _at_"\w" \\ ?????? ????
? ???????? ?? ??? MatchCollection matches
Regex.Matches( text, pattern,
RegexOptions.Multiline) foreach (Match match in
matches) Console.Write("0 ", match) //
?????? ??????? Multiline ?????????? ? ??????
?????
52
Escaping ?? ????????? ?????
  • ??? ????????? ???????????? ?? ????????? ?????
    ?????? ?? ?? escape-??? ?????? ?????, ?????? ??
    ?????? ?????????
  • ??? ???????? ??? ????? ?? ?????? ???? ?? ?????
    ?????? ???? ? ????? ?????
  • ????? ?? ?????? ???? ??? ??? ????? ??????????
    escaping-? ? ???????????? ?????? ?? ??????? "\w"
    ??? "."?
  • ????? ????? ?????? ??? ? ????? ????? ?? ????? ???
    ???????????? ?????? ?????? ????

String text _at_"?????? ????? ?? ?????? '????' ?
?????" _at_" ????? ????? ?? ??????? ?
?????????? ????? " _at_"\b(??)??(((?????)(??)
?)((?????)(??)?))\b." string word
Console.ReadLine() String pattern _at_"\b"
Regex.Escape(word) _at_"\b" Match match
Regex.Match(text, pattern)
53
???? ?? ???????? ???. ???????
  • ??????????? ?????? ?? ????????? ??????!
  • ??? ?????????? ?????? ????????? ??????, ?????????
    ???? ?? ?????? ? ?????? ?? ?? ????????? ???? 3
    ??????! ??? ???? 1 ???????
  • ? ?????? ?????? ?? ????? ?? ??? ????? ?? ??
    ?????????
  • ???? ??????? ?????????? ?????? ????????? ??????
  • ?????????? ???????? ?? ????? ? ?????? ??-??????
    ????????? ?????? ?? ????? ????
  • ???????? ?? ????????? ?? ?????? ????????? ?
    ?????? ????? ????? ???????? ???????????, ? ????
    ???? ????????? ???? ?? ?? ?????? ?????

54
??? ??????????????
  • ????????????? ?? ??????????? ?????? ???? ?? ????
    ???????? ?????, ??? ????? ?????????? ??????????
  • ???????? ???????? ??? ???? ?? ????? ?? ?????
    ????? ???? ??????? ????? ??????
  • ? ????? ?????? ??????????? ?????? ?? ??????????
    ?? ?????? "??????? ? ??????? ?????"
    (backtracking)
  • ??? ??????????? ????? ?????? ???? ? ????? ?????,
    ????????? ? ???????????

String text "aaabacabababaccbacbcbccacbcbccbaccc
ccc" string pattern _at_"(\wab\wac\waa\wc)
cccccc" Match m Regex.Match(text, pattern)
55
????????? ?????? ??? ???????
  • ??????? ?? ??????? ??? ???? ? ????? ???
  • ?????? ??????? ????? ?????, ?? ?????????
    ????????? ? 1, 2 ? 3
  • ???????? ?? ?????????? ltvargtltvaluegt

String text " ???? ????, ??? ?????!" string
pattern _at_"\A\s(\w)(\W)(\w)" string newText
Regex.Replace(text, pattern,"321") Console.W
riteLine(newText) // ???????? ???? ????, ???
?????!
String text "servermail.bg\nuseru1\npass!ment
e17" string pattern _at_"(\w?)\s\s(\S)\s" M
atchCollection matches Regex.Matches(text,
pattern) foreach (Match m in matches)
Console.WriteLine("var0 value1",
m.Groups1, m.Groups2)
56
????????? ?????? ??? ???????
  • ???????? ?? ????

String text "17.03.2004 121105" string
pattern _at_"\A(?ltdaygt\d) day in
the beginning (\.\/)
separator (. or /) (?ltmonthgt\d)
month (\.\/) separator (.
or /) (?ltyeargt(1920)?\d2) year (19XX,
20XX or XX) \s
whitespace (?lthourgt\d) hour
separator
(?ltmingt\d) minutes
((?ltsecgt\d))? seconds
(optional)" Match match Regex.Match(text,
pattern, RegexOptions.IgnorePatternWhitespace
) (???????? ??????????)
57
????????? ?????? ??? ???????
  • ???????? ?? ????
  • ?????????? ?? ???? ?? ????? ?? ????

if (match.Success) GroupCollection gr
match.Groups Console.WriteLine("day0
month1 year2\n" "hour3 min4
sec5", gr"day", gr"month",
gr"year", gr"hour", gr"min",
gr"sec") else Console.WriteLine("Inva
lid date and time!")
String fileName _at_"/home/nakov/sample.tar.gz" St
ring file Regex.Replace(fileName, _at_".(\\/)",
"")
58
??????? ?????? ???????
  • ?? ? ????? ?? ?????? ?????? ?? ??????????? ??????
  • ? ???????? ??? ???????, ?????? ?????? ?? ????????
    ?? ?????? ??????? ?? ??????
  • Regular Expressions Library http//www.regexlib.
    com/
  • 3 Leaf .NET Regular Expression Repository
    http//www.3leaf.com/resources/articles/regex.aspx
  • ?????? ?? ?????????? ??????????? ?? ????????? ?
    ???????? ?? ???. ?????? ????
  • The Regulator http//royo.is-a-geek.com/iseriali
    zable/regulator/
  • The Regex Coach http//www.weitz.de/regex-coach/

59
???????????? 3
  • ???????????? "The Regulator"

60
????????? ??????
????????
61
??????????
  1. ??????? ???????? ????? ????????????? ???????????
    ??????. ??? ?? ????????? ???????? ?? ????? ??
    ??????????? ??????? ????? ??????????? ??????????
  2. ??????? ???????? ?????????? ?? .NET Framework ??
    ?????? ? ????????? ?????? - ????????? ??????? ?
    ??-??????? ?? ??????.
  3. ???????? ????????, ????? ? ???????? ?? ?????????
    ????? ?? ?????? ???????????????? ?? ??????? (???)
    ? ????? ????? ??????? ?? ?????? ?????? ????,
    ????? ??????? ?????????? ??? ? ???? ?? ????
    ??????.
  4. ???????? ?? ? ???? ????????? ????? ?? ?? ???????
    ???? ??????? ? ????? ?????? ????? ?? ?????????
    ???????? (???? ?? ????? ???????? ????? ???
    ????????????? ?????????). ?????

62
??????????
  1. ???????? ????????, ????? ? ??????? ?? ???. ?????
    ???????? ?????? ????? ??? ?????? ????, ????????
    ?? ?????? ????. ???????? ??????? "0", "33",
    "-2381.78132", "4.3347", "12.00" ? "0.34" ??
    ?????? ?? ???????, ? ??????? "3", "--2", "24
    543", "01.23", "12.", "11,23", "12?7" ??
    ?????????.
  2. ???????? ????????, ????? ??????? ?? ????? ???????
    ???????? ?????? ?????????, ????? ???????? ??
    email ????? (????????????????? ?? ??????? ???
    ?????? ltidentifiergt_at_lthostgt... ltdomaingt).
    ??????????? ???????? ???. ?????.
  3. ???????? ????????, ????? ??????? ?? ????? ???????
    ???????? ?????? ??????, ????? ???????? ?? URL
    ?????? (????????? ??? ?????? www.lthostgt...ltdomaingt
    ? ?????????, ????????? ? "http//").

63
??????????
  1. ???????? ????????, ????? ? ??????? ?? ?????????
    ????? ?? ????? URL ????? ??? ??????
    protocol//server/resource ??????? ?? ????
    ????????? ?? ???????? protocol, server ?
    resource. ???????? ?? URL http//www.devbg.org/f
    orum/index.php ?????? ?? ??????? protocol
    "http", server "www.devbg.org" ? resource
    "/forum/index.php".
  2. ????? ? ?????? ? ????, ????? ???????????? ?????
    ??? ?????? "???? ????????", ?? ???? ?????????
    ??????? ?? ????? ??? (?????????? ???? ?? ??
    ?????? ?? ??????? ????). ?? ?? ??????? ????????,
    ????? ?? ?????? ???? ?????? ?????????? ? ?
    ???????. ??????????? ????????? ?????? ? ????? ??
    ??????? ?? ??????.

64
??????????
  1. ???????? ????????, ????? ????????? ????? ????? ??
    ?????? ???? ? ?????? ? ????????? ??????
    ?????????, ? ????? ???? ???? ?? ?????. ?????? ??
    ???????, ?? ????? ??????? ?? ????? ?? ?????????
    ".", "!" ? "?" ???????? ???? ?? ?????????.
    ???????? ? ?????? "\t????? ?? ????! ???????
    ?????? ?????? ???? ????????? ??????. ?? ?????
    ???-??? ? ????????.\n \t ?????? ?? ???????? ??
    ??? ????." ?????? "????" ?? ????? ???? ? ???????
    ? ?????????? ?????????. ?? ????????? ??
    ??????????? ???? ?? ????? ??????????? ?????????
    ??????. ???????? ?? ? ??????? "\s(.\s)(\.\!\?
    )" ? ?????
  2. ???????? ????????, ????? ??????? ?? ????? ?????
    ?????? ???? ????? ??? ???? ? ?? ??????? ? ?????
    ?? ???????? ??????. ?? ????? ??????????? ??????
    Regex.Split.

65
??????????
  1. ???????? ????????, ????? ?????? ? ????? HTML
    ???????? ?????? ??????????? lta href...gt...lt/?gt ?
    ???????????? ?? ???? ?????? ??? ?????? url
    href....../url. ?????????? ?????? ?? ??
    ?????? ? ??????? ?????? ? ???? ? ???????
    ??????????? (??????? ?? ???? ?? ? ????????? ?
    ????? HTML). ??????????? ????????? ?????? ?
    ?????? Regex.Replace.
  2. ???????? ????????, ????? ?????? ?????? ? ??????
    ????????? ? ??????? ???. ???????? ???????????
    "????? ???? ??????." ?????? ?? ?? ??????????? ?
    "?????? ???? ?????.". ??????????? ?????
    Regex.Replace ?????? ? MatchEvaluator

66
?????????? ??????????
  • Yashavant Kanetkar, The Regular Expressions
    http//www.funducode.com/csharpart/csarticle30.ht
    m
  • Brad Merrill, C Regular Expressions
    http//windows.oreilly.com/news/csharp_0101.html
  • MSDN Library http//msdn.microsoft.com
Write a Comment
User Comments (0)
About PowerShow.com