サイトメニュー
|
|
String text = "Hello World!";
Pattern p = Pattern.compile("Hello");
Matcher m = p.matcher(text);
if (m.find()) {
} else {
}
String text = "www.hoge.net";
Pattern p = Pattern.compile("\\.net");
Matcher m = p.matcher(text);
String replaced = m.replaceFirst(".info");
System.out.println(replaced);
Matcher m = p.matcher(st);
String replaced = m.replaceAll("");
String text = "Name: Regular Expressions";
Pattern p = Pattern.compile("^(\\w+):\\s+(.+)$");
Matcher m = p.matcher(text);
if (m.find()) {
String key = m.group(1); // key = "Name";
String value = m.group(2); // value = "Regular Expressions";
System.out.println("key = " + key);
System.out.println("value = " + value);
}
String text = "<a href=\"http://www.hoge.net/\">HOGE</a>";
String replaced = text.replaceFirst("<a href=\"(.+?)\">(.+?)</a>",
System.out.println(replaced);
Pattern p = Pattern.compile("(\\p{InBasicLatin}+|"
+ " \\p{InHiragana}+|" + " \\p{InKatakana}+|"
+ " \\p{InCJKUnifiedIdeographs}+)", Pattern.COMMENTS);
Matcher m = p.matcher(text);
while (m.find()) {
String chunk = m.group(1);
System.out.println(chunk);
}
String replaced = text.replaceAll("<.+?>", "");
System.out.println(replaced); |