root/trunk/myweb/src/mypackage/StringTest.java

Revision 52, 1.3 KB (checked in by yabuki, 2 years ago)
  • Property svn:mime-type set to text/plain
Line 
1package mypackage;
2
3import java.util.regex.*;
4
5public class StringTest {
6  public static void main(String[] args) {
7    StringBuffer sb=new StringBuffer("文字列の始まり");
8    if (true) sb.append("場合1");
9    else sb.append("場合2");
10    sb.append("終わり");
11    System.out.println(sb.toString());
12
13    String str="<h3>文字列</h3><p>文字列を扱うには(中略)</p>"
14              +"<H3>正規表現</h3><P>先に説明したStringや\n(後略)</p>";
15    //Pattern p=Pattern.compile("(?i:<h3>.*?</h3>)");
16    //Pattern p=Pattern.compile("(?is:<p>.*?</p>)");
17    Pattern p=Pattern.compile("(<p>.*?</p>)",Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
18    Matcher m=p.matcher(str);
19    while (m.find()) System.out.println(m.group());
20
21    String[] strs={"aaXbbXX", "cXXd", "eXXXf"};
22    for (String s:strs) System.out.println(s.replaceAll("X+","Z"));
23
24    String[] strs2={
25      "yabuki@example.com",
26      "taro@example.org",
27      "taro.yabuki@unfindable.net"
28    };
29
30    for (String s:strs2)
31      System.out.println(s.replaceFirst("(.+)@.+\\.(.+)","$1 のドメインは $2"));
32
33    Pattern p2=Pattern.compile("(.+)@.+\\.(.+)");
34    Matcher m2;
35    for (String s:strs2) {
36      m2=p2.matcher(s);
37      m2.find();
38      System.out.println(m2.group(1)+" のドメインは "+m2.group(2));
39    }
40  }
41}
Note: See TracBrowser for help on using the browser.