OPI_LR2011_ch2m1
.pdf51
Insert() – ] '] ^; Remove() – ] ;
Replace() – \ ]
.
h ( ( ^ ^ StringBuilder ] , \
'] StringBuilder ] -
, . ?
StringBuilder . @
string StringBuilder.
// Z P P TQ^
string s1 = "r R ,oSP, ZR, p Z QY Z^";
//QP P\< Z Q T ST R<S R P P TQ^ const char cspace = ' ';
const char ccomma =',';
//S Zk\< <P RS Z Q T SRs R<S TS
//T T TiTP TQ^\< l _ S <P < P<R char[] delimiters = { cspace, ccomma }; StringBuilder output = new StringBuilder(); int ctr = 1;
//SR T \< T Z R P ST Z Q T [ R TS
//T QY ZT_P\<
foreach (string substring in s1.Split(delimiters))
{
//Appendformat P\ Z S _ q Z<P ^ output.AppendFormat("{0}: {1}\r\n", ctr++, substring);
}
// SRST Z Q^ [ P ^
textBox5.Text = output.ToString();
• , Y ( -. ? ] (, Y -
. h
StringBuilder.
StringBuilder output = new StringBuilder();
h '] StringBuilder (
, . ' ( ]
AppendFormat(). h ] + -
+. ] + , -
. ' Y – ,
– .
output.AppendFormat("{0}: {1}\r\n", ctr++, substring);
' -
+ ,
52
+, +
StringBuilder +
:
textBox5.Text = output.ToString();
? Y ] (, \ Y:
1:r R
2:oSP
3:ZR
5:p
7:Z QY Z^
3.2.8 951 62 4 "7 #7
? – Y ( -
) . ^] ^
Y Y. (
«*» «?», \ ^ DOS. ' Y ] Y , (
– . =, Y Y
«text?.*» + textf.txt, textl.asp Y , -text.txt text.htm. • \ DOS
(, Y ,
, Y Y
.
] Y -
, , \ ^ ,
, Y (C, C++, Assembler).
3.2.8.1 " 2 51 62 7Z "7 #4"
• , ^ ^
:
Y ;
Y Y ;
Y .
2# ] :
Regex re = new Regex ("QZPQ ", " iT‚');
53
MatchCollection mc = re.Matches("Z `^ ^'); iCountMatchs = mc.Count;
r – '] R g . ] -
Y . < ( 3.2, \ ]
Y.
= 3.2 –
2 |
|
|
i |
' Y . |
|
m |
S (, \ ] |
|
, . |
||
|
||
n |
|
|
+ (?<name>...). |
||
|
||
c |
9 ^]. o ] ( s l-9, - |
|
^] Y . |
||
|
||
s |
h ] - |
|
- . 0 \ ] (. |
||
|
||
x |
@ ^ ] ( - |
|
, . .). |
||
|
||
r |
@ ] Y - . |
9 m s ] ( ( , \
] , \ ] ,
^ ^ .
@ , \ (
RegularExpressions , \ System.Text:
using System.Text.RegularExpressions;
3.2.8.2 "7 7 8 7 51 62 7Z "7 #4". 6 7 7!-
" 64" (Character classes)
@ ( ,
(.
@ ^ (, (
( ^ ) Y. <, « [ -](» ] «(» «(», , \ - ^ « », « » « », \ ^ «(».
54
( , ( , -
. =, '[^1-6]' ,
+ 1 6. 2 , \ '\b'
] backspace ( ).
3.2.8.3 " 7<48 7 (Quantifiers)
• \ , Y -
, ( , \ ^ -
+ (quantifiers).
<, ( «hel+o», \ , \ ] «he»,
«1», \ ] « ». 2 , \ + -
, .
' + ( (-
3.3.
= 3.3 – / +.
2 /
*@ ] 0 Y (
. <, 'zo*' ] "z" "zoo".
+@ ] 1 Y . <-
, "zo+" ] "zo" "zoo", "z".
?@ ] 0 1 . <, 'do(es)?'
] "do" "dorf "does".
{n} |
n – . @ ] (. |
|
<, ' {2}' " " "Bob", " " |
||
|
"food". |
|
|
n – . @ ] ( ^, |
|
{n,} |
Y n . <, ' {2,}' " " "Bob", |
|
" " "foooood". ' {1,}' |
||
|
||
|
' +'. ' {0,}' ' *'. |
|
|
n m – , n <= m. @ ] |
|
{n,m} |
n m (. <, ' {1,3} |
|
Y " " "fooooood". ' {0,1}' ' ?'. '- |
||
|
||
|
( ^ + |
55
3.2.8.4 84 K5 2 K 87 2384"
' ^
^ $. <, «^thing» ] , \ - ] «thing». «thing$» ] , \ ] «thing». { ^^ ^ 's'. ' ^
's' . 6 -
( , ^ escape-
\6 \Z.
3.2.8.5 ` 7n2 6 "
h ^ '\b'
'\@'.
Regex re = new Regex ("<", "ms");
re ] « »
« », « » « ». €
, ( :
Regex re = new Regex ("\b<", "ms");
= « » . <
, \ '\b' ] backspace ( ).
< ( 3.4 Y ^ Y-
^ -
. @ ^ ^ . <,
], \ – . 'FTP' ]
«FTP», \ ^ .
= 3.4 – .
2 |
|
^ |
' . |
$ |
9 , \n ( . ^ m). |
\6 |
' (ignores the m option). |
\Z |
9 , \n (] ^ |
|
m). |
\z |
= (] ^ m). |
\G |
' Y ( |
|
Y). |
|
56 |
' ( 3.4 |
|
|
|
2 |
|
\b |
< ( \w (+-+) \W (+- |
|
-+) . ' ] true Y |
|
, . |
\@ |
< \b- . |
\w |
2. = (, \ [a-za-z_0-9]. |
\W |
, . (, \ [Aa-za-Z_0-9]. |
\s |
S- ( ] . = (, \ [ \f\n\r\t\v]. |
\S |
S- . = (, \ [ \f\n\r\t\v]. |
\d |
h +. (, \ [0-9]. |
\D |
< +. = (, \ [0-9]. |
3.2.8.6 4 n4o 1 " 2. A "76 3 "7 51 62 7Z "7 #4"
2 '|' ( -
. @ ( – '(…|...|...)' – ]
. h ( ^ « » -
Y (
$1, $2,..., $9.
Regex re = new Regex("like (apples|pines|bananas)"); MatchCollection mc = re.Matches("I like apples a lot");
= ^ «like apples», «apples» — . h ( ( «apples» $1 - Y . @ ] .
' :
1)- ] , \ ] -
. • \ ^ , -
'\';
2)] ;
3)( ( ( ) \ ]
( '[]', , \ ( -
( . • \ Y ( ] '^',
], \ ( ( -
. ( ( '-', \ -
57
] . <, a-z – -
+, 0-9 - + . ;
4), , ( -
^ '\';
5)^ '|'. '
( .
h
. 3.5.
= 3.5 – '
? |
/ |
s/(\S+)(\s+)(\S+)/$3$2$1/ |
' Y |
m/(\w+)\s*=\s*(.*?)\s*$/ |
' Y name=value. = ' - |
|
] – $1, – $2. |
m/(\d{4})-(\d\d)-(\d\d)/ |
0 + YYYY-MM-DD. |
|
YYYY – $1, MM – $2, DD – $3. |
m/^.*(\\|\/) |
@ Y +. <- |
|
, |
|
"Y:\KS\regExp\!.Net\Compilation\ms- |
|
6D(1).tmp" |
|
"Y:\KS\regExp\!.Net\Compilation\" |
("(\\"|\\\\|[^"])*"|/\*.*\*/|//[ |
+ - |
^\r]*|#\S+|\b(new|static |
2++, ] , |
char|const)\b) |
+ "new", "static char" "const" |
<\s*a("[^"]*"|[^>])*> |
@ ] <a href="…"> HTML- |
3.2.9 78 7 2 51 62 7Z "7 #4": Regex
.Net ] ']-] -
.
S Ÿ ]
System.Text.RegularExpressions. o
] Regex, ] ^ -
. 9 , Regex ]
. @ Regex ^
:
58
string s1 = "r R ,oSP, ZR, p Z QY Z^"; Regex theregex = new Regex(" |, |,"); StringBuilder sbuilder = new StringBuilder(); int id = 1;
foreach (string substring in theregex.Split(s1))
{
sbuilder.AppendFormat("{0}: {1}\r\n", id++, substring);
}
textBox1.Text = sbuilder.ToString();
? :
1:r R
2:oSP
3:ZR
4:p
6:Z QY Z^
• , ] «/,h,=, ? ». s1. h
, ] - Y-:
regex theregex = new Regex(" |, |,");
] , ']
6S/. ] , , , \ -
. ?, \ ] , ]
'] theregex.
9 Regex Split, ]
string.Split @ ] Y -
. ( , -
^ String Builder, + ] .
Split Regex ( ( -
. ' Y
. h — .
string s1 = "r R ,oSP, ZR, p Z QY Z^"; StringBuilder sbuilder = new StringBuilder(); int id = 1;
foreach (string substring in Regex.Split(s1, " |, |,"))
{
sbuilder.AppendFormat("{0}:{1}\r\n", id++, substring);
}
textBox2.Text = sbuilder.ToString();
? ] ( . @ ] , \
^ '] Regex
Y Regex.Split, ^]
59
– , Y, -
.
9 , Split ( \ ,
^ ( Split
^ Y.
3.2.10 78 7 2 Match 8 658n4
' , ^ ^ -
Y . 9-
] '] MatchCollection, \ -
'] Match. /'] Match ( (
, ^ ( (Length) (Value). h Y:
string s1 ="„ Z `^ ^";
//Q Pl R Y^ [-Rl Z YT [ Rl R<S
//P ^ Rl QP Z YT [ R<
Regex thereg = new Regex(@"(\S+)\s");
//ZtP R iTk Z Q^ [ P ^ `^ ^
MatchCollection thematches = thereg.Matches(s1);
//Z YTZ ^ T\‚ iT‚
foreach (Match thematch in thematches)
{
textBox3.Text = textBox3.Text + "thematch.Length: " + thematch.Length + "\r\n";
if (thematch.Length!= 0)
{
textBox3.Text = textBox3.Text + "thematch: " + thematch.ToString() + "\r\n";
}
}
? :
thematch.Length: 3 thematch: „ thematch.Length: 6 thematch: Z thematch.Length: 4 thematch:
? Y. 2 ^]
Y:
string s1 = "„ Z `^ ^";
' + ] :
Regex thereg = new Regex(@"(\S+)\s");
60
{ Y . (\S) ]
- . (+), \ (\S), - ], \ ( - . (\s) ( - . , ]: « , ^ -
^ ».
? ( ] Y Y
. 0 Y ^ ^, \
] . • \ -
, «Y» (
.
3.3"3 2 3 7
3.3.1/
^ , ^ , ( -
.
3.3.2@ :
:
1.? : , \ ^^,
– {0 ' ^},
: «fehhh eryaaa»
«feh3 erya3». = ( +-
^ .
2.? : , \ , ]-
^, ] , -
. ? + (/ * - +). <, «2 ^ 5»
«2 + 5», –
«7».
1 :
1.' +: Empty, Length, Split.
2.' +: Compare, 2l n , StartsWith.