Вы находитесь на странице: 1из 14

(Regular Expressions)

;
,
;
,
;
,
.


.
^ , ( ,
" <->").
\d ( [0-9]).
\D , ( [^0-9]).
\s - ( [ \n\t\f\r]).
\S , -
( [^ \n\t\f\r]).
\w ( [a-zA-Z_0-9]).
\W ,
( [^\w]).

/

c |
()

[]


[^]

()
?
0 1
*
0
+
1
{m}
m
{m,}
m
{m,n}
m n

String


-
String:
matches(String regex)
regex.
split (String regex)
regex.
replaceAll(String regex, String replacement),
.
replaceFirst(String regex, String replacement),

.

Regex- Java

java.util.regex :
Pattern,
.
Matcher,
.
PatternSyntaxException,
, -
,
.

Pattern

:
Pattern myPattern =
Pattern.compile("regex");
Matcher
Mather myMatcher =
Pattern.matcher ("line");
Matcher
line

Pattern


matcher() Matcher.

matches()
.
boolean b = myMatcher.matches();


Pattern pattern = Pattern.compile("a.*string");
Matcher matcher = pattern.matcher("a string");
boolean didMatch = matcher.matches();
System.out.println(didMatch);

atcher
( )
.
,
Matcher:
boolean find() boolean find(int start)
,
, .
start .
String group() ,
;
start() ,
.
end() ,

.


String regex = "(\\w+)@(\\w+\\.)(\\w+)(\\.\\w+)*";
String s = " .:mymail@tut.by rom@bsu.by";
Pattern p2 = Pattern.compile(regex);
Matcher m2 = p2.matcher(s);
while (m2.find())
System.out.println("e-mail: " + m2.group());


String input = "2+2=4";
Pattern pattern = Pattern.compile("\\d");
Matcher matcher = pattern.matcher(input);
System.out.println("Before: " + input);
String result = matcher.replaceAll("");
System.out.println("After: " + result);


(777) 777-777-77
\\([0-9]{3}\)\\s+[0-9]{3}\-[0-9]{3}\-[0-9]{2}

[0-9]?\\.[0-9]+
E-mail
[a-z0-9._%-]+@[a-z0-9._%-]+\\.[a-z]{2,4}

Вам также может понравиться