利用compile和matcher,并把match结果保存在Vector中。System.out.println("Matcher"); /* X+ X, one or more times */ Matcher matcher = Pattern.compile("[a-zA-Z]+\\d+").matcher(str); Vector<String> vector = new Vector<String>(); while (matcher.find()) { vector.add(matcher.group()); } for (int j = 0; j < vector.size(); j++) { System.out.println(vector.get(j)); }
经测试后的完整代码:package com.han;import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern;public class StringSplitAndMatcher { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str = "G11G12J1J2";
/* If n is zero then the pattern will be applied as many times as * possible, the array can have any length, and trailing empty * strings will be discarded. */ String[] splitResult = Pattern.compile("(?=[a-zA-z])(?<=[0-9])").split(str, 0); System.out.println("The splitResult length is: " + splitResult.length); for (String e : splitResult) { System.out.println(e); }
/* regex = regular expression */ String[] splitResult2 = str.split("(?<=[0-9])(?=[a-zA-z])"); System.out.println("The splitResult2 length is: " + splitResult2.length); for (String e : splitResult2) { System.out.println(e); }
/* regex = regular expression */ String[] splitResult3 = str.split("(?<=\\d)(?=[a-zA-z])"); System.out.println("The splitResult3 length is: " + splitResult3.length); for (String e : splitResult3) { System.out.println(e); }
/* X+ X, one or more times */ Matcher matcher = Pattern.compile("[a-zA-Z]+\\d+").matcher(str); Vector<String> vector = new Vector<String>(); while (matcher.find()) { vector.add(matcher.group()); } System.out.println("The matcher length is: " + vector.size()); for (int j = 0; j < vector.size(); j++) { System.out.println(vector.get(j)); }
Matcher matcher2 = Pattern.compile("[a-zA-Z]+[0-9]+").matcher(str); Vector<String> vector2 = new Vector<String>(); while (matcher2.find()) { vector2.add(matcher2.group()); } System.out.println("The matcher2 length is: " + vector2.size()); for (int j = 0; j < vector2.size(); j++) { System.out.println(vector2.get(j)); }
String str = "G11G12J1J2";//false
Matcher m=Pattern.compile("[a-zA-Z]+\\d+").matcher(str);
while(m.find()){
System.out.println(m.group());
}
String[] strs = str.split("(?<=[0-9])(?=[a-zA-Z])");
String str="G11G12J1J2";
String[] strs = str.split("(?<!^)(?=[A-Z])");
System.out.println(Arrays.toString(strs));
/* X+ X, one or more times */
Matcher matcher = Pattern.compile("[a-zA-Z]+\\d+").matcher(str);
Vector<String> vector = new Vector<String>();
while (matcher.find()) {
vector.add(matcher.group());
}
for (int j = 0; j < vector.size(); j++) {
System.out.println(vector.get(j));
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class StringSplitAndMatcher {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "G11G12J1J2";
/* If n is zero then the pattern will be applied as many times as
* possible, the array can have any length, and trailing empty
* strings will be discarded. */
String[] splitResult = Pattern.compile("(?=[a-zA-z])(?<=[0-9])").split(str, 0);
System.out.println("The splitResult length is: " + splitResult.length);
for (String e : splitResult) {
System.out.println(e);
}
/* regex = regular expression */
String[] splitResult2 = str.split("(?<=[0-9])(?=[a-zA-z])");
System.out.println("The splitResult2 length is: " + splitResult2.length);
for (String e : splitResult2) {
System.out.println(e);
}
/* regex = regular expression */
String[] splitResult3 = str.split("(?<=\\d)(?=[a-zA-z])");
System.out.println("The splitResult3 length is: " + splitResult3.length);
for (String e : splitResult3) {
System.out.println(e);
}
/* X+ X, one or more times */
Matcher matcher = Pattern.compile("[a-zA-Z]+\\d+").matcher(str);
Vector<String> vector = new Vector<String>();
while (matcher.find()) {
vector.add(matcher.group());
}
System.out.println("The matcher length is: " + vector.size());
for (int j = 0; j < vector.size(); j++) {
System.out.println(vector.get(j));
}
Matcher matcher2 = Pattern.compile("[a-zA-Z]+[0-9]+").matcher(str);
Vector<String> vector2 = new Vector<String>();
while (matcher2.find()) {
vector2.add(matcher2.group());
}
System.out.println("The matcher2 length is: " + vector2.size());
for (int j = 0; j < vector2.size(); j++) {
System.out.println(vector2.get(j));
}
}}