string s = [sdkdfsjl skdfjdlsj kdjflskfj];
//[]之间的 空格内容替换成&
Pattern pp = Pattern.compile("???");
Matcher mm = pp.matcher(sr);
String tepp = "";
String tempp = "";
while(m.find()){
tepp = mm.group();
tempp = tepp.replaceAll(" ","\\$");
System.out.println(tepp+"------");
sr = sr.replace(tepp, tempp);
}
各位问下:???中该怎么写呀谢谢
刚才<>之间的???写:<(.*?)>就可以,可是对[]这样写:[(.*?)]就不可以。
谢谢
//[]之间的 空格内容替换成&
Pattern pp = Pattern.compile("???");
Matcher mm = pp.matcher(sr);
String tepp = "";
String tempp = "";
while(m.find()){
tepp = mm.group();
tempp = tepp.replaceAll(" ","\\$");
System.out.println(tepp+"------");
sr = sr.replace(tepp, tempp);
}
各位问下:???中该怎么写呀谢谢
刚才<>之间的???写:<(.*?)>就可以,可是对[]这样写:[(.*?)]就不可以。
谢谢
public static void main(String[] args) {
String s = "[sdkdfsjl skdfjdlsj kdjflskfj]";
Pattern pp = Pattern.compile("\\[(.*?)\\]");
Matcher mm = pp.matcher(s);
while (mm.find()) {
System.out.println(mm.group(1).replaceAll(" ", "\\$"));
}
}
报错:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 8
[sfdsdf
^
at java.util.regex.Pattern.error(Pattern.java:1713)
字符串搞死人呀
public static void main(String[] args) {
String s = "[sdkdfsjl skdfjdlsj kdjflskfj]";
Pattern pp = Pattern.compile("\\[(.*?)\\]");
Matcher mm = pp.matcher(s);
while (mm.find()) {
System.out.println(mm.group(1).replaceAll(" ", "\\$"));
}
}
你按我这试了吗?
//[]之间的 空格内容替换成¥
Pattern pp = Pattern.compile("^[\\[](.*?)[\\]]$");
Matcher mm = pp.matcher(sr);
String tepp = "";
String tempp = "";
while(mm.find()){
tepp = mm.group(1);
tempp = tepp.replaceAll(" ","\\$");
System.out.println(tepp+"------");
sr = sr.replace(tepp, tempp);
}
我的也单独测试是正确的,我是读取文件,文件内容:
sdfjdslkfjd [sfdsdf dfsdf sdfsdf] <dsfjfs kdjfs; kdjfsk>
然后就报错:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 8
[sfdsdf
^
at java.util.regex.Pattern.error(Pattern.java:1713)
public static void main(String[] args) {
String sr = "ffs da fds[fdas fdfd sa] [sdkdfsjl skdfjdlsj kdjflskfj]fda[fs] f[]";
Pattern p = Pattern.compile("\\[(.*?)\\]");
Matcher m = p.matcher(sr);
StringBuilder sb = new StringBuilder() ;
int lastIndex = 0 ;
while(m.find()){
sb.append(sr.substring(lastIndex, m.start(1))) ;
sb.append(m.group(1).replace(" ", ""));
lastIndex = m.end(1);
}
sb.append(sr.substring(lastIndex));
System.out.println(sb);
}
ffs da fds[fdasfdfdsa] [sdkdfsjlskdfjdlsjkdjflskfj]fda[fs] f[]
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 83
[0$"Mozilla/4.0$(compatible;$MSIE$6.0;$Windows$NT$5.1;$SV1;$Foxy/2)"$1024x768$"zh-cn"$1$32$10.0$"modem"$8]
^
at java.util.regex.Pattern.error(Pattern.java:1713)