日期:2014-05-20 浏览次数:20924 次
(\[\w+\])|(\<i\>\w+\<\/i\>)
------解决方案--------------------
String s = "abcdef[wx]hijk<i>aaa</i>lmn[fn]opq<i>bbbb</i>rst"; String px = "[a-z]*(\\[[a-z]*\\])[a-z]*(<i>[a-z]*</i>)"; Pattern p = Pattern.compile(px); Matcher m = p.matcher(s); while (m.find()) { System.out.println(m.group(1)); System.out.println(m.group(2)); }
------解决方案--------------------
String scontent="abcdef[wx]hijk<i>aaa</i>lmn[fn]opq<i>bbbb</i>rst"; String rex = "(\\[\\w+\\])|(\\<i\\>\\w+\\<\\/i\\>)"; Pattern p = Pattern.compile(rex); Matcher m = p.matcher(scontent); while(m.find()){ System.out.println(m.group()); }
------解决方案--------------------
System.out.println(m.group()+m.start());起始值索引
------解决方案--------------------
for example
String s = "abcdef[wx]hijk<i>aaa</i>lmn[fn]opq<i>bbbb</i>rst"; Pattern p = Pattern.compile("\\[\\w+\\]|(?i)<i\\s*>.*?</i>"); Matcher m = p.matcher(s); while (m.find()) { System.out.println(m.group()); }
------解决方案--------------------
String s = "abcdef[wx]hijk<i>aaa</i>lmn[fn]opq<i>bbbb</i>rst"; Pattern p = Pattern.compile("\\[\\w+\\]|(<i>\\w+</i>)"); Matcher m = p.matcher(s); while(m.find()) { System.err.println("index = " + m.start() + ", string = " + m.group()); }
------解决方案--------------------
String str="abcdef[wx]hijk<i>aaa</i>lmn[fn]opq<i>bbbb</i>rst"; Pattern pat=Pattern.compile("(?:\\[.*?\\]|<(.*?)>[^<]+</\\1>)"); Matcher mcr=pat.matcher(str); while(mcr.find()){ System.out.println(mcr.start()+" "+mcr.group()+" "+mcr.end()); }