String a="PartA PartB \"2006-11-13 8:00:00\" \"2006-11-13 9:30:00\" PartC"
请问如果写正则表达式进行分割出下面的数组出来:
PartA
PartB
"2006-11-13 8:00:00"
"2006-11-13 9:30:00"
PartC
也就是说凡是引号里面的空格就不进行分割了。知道的朋友帮下忙,先谢谢了!
请问如果写正则表达式进行分割出下面的数组出来:
PartA
PartB
"2006-11-13 8:00:00"
"2006-11-13 9:30:00"
PartC
也就是说凡是引号里面的空格就不进行分割了。知道的朋友帮下忙,先谢谢了!
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Splitter {
public static void main(String[] args) {
String a="PartA PartB \"2006-11-13 8:00:00\" \"2006-11-13 9:30:00\" PartC";
split(a);
} private static void split(String s) {
String regex = "[\"]{1}[^\"]*[\"]{1}|[\\S]+";
System.out.println(regex);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher( s);
while(m.find()){
String ss = m.group();
System.out.println(ss);
}
}}
的api,自己写一个,是不是用正则?
如:
<aa>
<bb>111
</bb>
</aa>要取得getElementByTagName("bb"),是不是用正则匹配<bb></bb>中的内容,这个该怎么写?要取得getElementByTagName("aa"),用正则匹配<aa></aa>中的内容
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Splitter {
public static void main(String[] args) {
String a="PartA PartB \"2006-11-13 8:00:00\" \"2006-11-13 9:30:00\" PartC";
split(a);
//T.show(splitV2(a));
} private static void split(String s) {
String regex = "[\"]{1}[^\"]*[\"]{1}|[\\S]+";
System.out.println(regex);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher( s);
while(m.find()){
String ss = m.group();
System.out.println(ss);
}
}
private static String[] splitV2(String s){
List resList = new ArrayList();
int posIndex = 0;
int[] pos = new int[2];
String temp = "";
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(c==' '&&posIndex ==0){
if(!temp.equals("")){
resList.add(temp);
temp="";
}
continue;
}
if(c=='"'){
temp+=c;
pos[posIndex++]=i;
if(posIndex==2){
posIndex = 0;
resList.add(temp);
temp="";
}
continue;
}
temp+=c;
}
resList.add(temp);
return (String[])resList.toArray(new String[0]);
}}