java.util.regex.Pattern類の使用例
- import java.util.regex.Pattern;
-
- public class SplitDemo {
-
- private static final String REGEX = ":";
- private static final String INPUT = "one:two:three:four:five";
-
- public static void main(String[] args) {
- Pattern p = Pattern.compile(REGEX); //
- String[] items = p.split(INPUT); // 。
- for(String s : items) {
- System.out.println(s);
- }
-
- System.out.println("#############");
-
- Pattern str=Pattern.compile(":");
- String[] strArray=str.split(INPUT,3 );
- for(String s:strArray){
- System.out.println(s);
- }
-
- System.out.println("#############");
-
- String[] strArray2=str.split(INPUT,-3 );
- for(String s:strArray2){
- System.out.println(s);
- }
- }
- }
運転結果:one three four five.