StringTokenizer(String str, String delimiters);
StringTokenizer(String str, String delimiters,
Boolean delimAsToken);
:
String nextToken() String ;
boolean hasMoreTokens() true, ;
int ountToken() .
. .
java.util.regex.Pattern (), , , . . java.util.regex.Matcher.
.
, , , - , ( ) , :
[abc] | a, b c |
[^abc] | , a, b c |
[a-z] | a z |
[a-d[m-p]] | a d, m p |
[e-z&&[dem]] | e m () |
, :
. | |
\d | [0-9] |
\D | [^0-9] |
\s | [ \t\n\x0B\f\r] |
\S | [^\s] |
\w | [a-zA-Z_0-9] |
\W | [^\w] |
\p{javaLowerCase} | ~ Character.isLowerCase() |
\p{javaUpperCase} | ~ Character.isUpperCase() |
:
ab | a b |
a|b | a b |
(a) | a |
, , .
, . . . .
a? | a |
a* | a |
a+ | a |
a{n} | a n |
a{n,} | a n |
a{n,m} | a n m |
, ? (, ) + (, ) . , .
Pattern . Matcher , .
Pattern :
Pattern compile(String regex) Pattern, regex.
Matcher matcher(CharSequence input) Matcher,
input.
|
|
boolean matches(String regex, CharSequence input) input regex.
String pattern() , .
String[] split(CharSequence input) input, , .
String[] split(CharSequence input, int limit) input limit .
matches() Pattern , , , , , Pattern . Matcher.
Matcher . - IllegalStateException. Matcher, :
boolean matches() , ;
boolean lookingAt() , ;
boolean find() boolean find(int start) , , . start .
Matcher , reset() reset(CharSequence input), .
, , replaceAll(String
replacement).
, region(int start, int end), regionEnd() regionStart(). :
Matcher useAnchoringBounds(boolean b) true, ^ $ .
boolean hasAnchoringBounds() .
, . ( ) . . . .
int end() , ;
int end(int group) ;
String group() , ;
String group(int group) ;
int groupCount() ;
int start() , ;
int start(int group) ;
boolean hitEnd() , .
Pattern Matcher , .
/* # 14: : DemoRegular.java */package chapt07;
|
|
import java.util.regex.*;
public class DemoRegular {
public static void main(String[] args) {
//
Pattern p1 = Pattern. compile ("a+y");
Matcher m1 = p1.matcher("aaay");
boolean b = m1.matches();
System. out. println(b);
// ,
String regex =
"(\\w+)@(\\w+\\.)(\\w+)(\\.\\w+)*";
String s =
" .:[email protected] [email protected]";
Pattern p2 = Pattern. compile (regex);
Matcher m2 = p2.matcher(s);
while (m2.find())
System.out.println("e-mail: " + m2.group());
//
Pattern p3 = Pattern. compile ("\\d+\\s?");
String[] words =
p3.split("java5tiger 77 java6mustang");
for (String word: words)
System.out.println(word);
}
}
:
True
e-mail: [email protected]
e-mail: [email protected]
Java
Tiger
Java
Mustang
, , .
/* # 15: : Groups.java */package chapt07;
public class Groups {
public static void main(String[] args) {
String input = "abdcxyz";
myMatches("([a-z]*)([a-z]+)", input);
myMatches("([a-z]?)([a-z]+)", input);
myMatches("([a-z]+)([a-z]*)", input);
myMatches("([a-z]?)([a-z]?)", input);
}
public static void myMatches(String regex,
String input) {
Pattern pattern = Pattern. compile (regex);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
System. out. println("First group: "
+ matcher.group(1));
System. out. println("Second group: "
+ matcher.group(2));
} else
System. out. println("nothing");
System. out. println();
}
}
:
First group: abdcxy
Second group: z
First group: a
Second group: bdcxyz
First group: abdcxyz
Second group:
Nothing
, .
, . . .
, , .
, . . .
Matcher .
Matcher appendReplacement(StringBuffer sb, String
replacement) sb. start()-1 , sb replacement. , end() .
StringBuffer appendTail(StringBuffer sb) sb. , appendReplacement().
java.util.Locale , . , . , : Locale.US, Locale.FRANCE. Locale :
Locale myLocale = new Locale("bel", "BY");
:
|
|
Locale current = Locale.getDefault();
, , new Locale("ru", "RU"), ( )
current.getCountry(); //
current.getDisplayCountry(); //
current.getLanguage(); //
current.getDisplayLanguage(); //
:
RU
Ru
, , . java.util.ResourceBundle Locale. ResourceBundle ( .properties). ResourceBundle , , parent. text. , , ,
. , text_de_CH.properties Locale, (de) (CH).
Text.properties
Text_ru.properties
Text_de_CH.properties