Stirng[] s1;//提取<>里的内容
String[] s2;//提取[]里的内容
String ss = s;
int i,j;
while(s.indexOf("<") != -1 ){
s1[i] = new String(s.substring(s.indexOf("<"),s.indexOf(">")));
s = s.substring(">");
i++;
}
while(ss.indexOf("[") != -1 ){
s2[j] = new String(ss.substring(ss.indexOf("["),ss.indexOf("]")));
ss = ss.substring("]");
j++;
}
String[] s2;//提取[]里的内容
String ss = s;
int i,j;
while(s.indexOf("<") != -1 ){
s1[i] = new String(s.substring(s.indexOf("<"),s.indexOf(">")));
s = s.substring(">");
i++;
}
while(ss.indexOf("[") != -1 ){
s2[j] = new String(ss.substring(ss.indexOf("["),ss.indexOf("]")));
ss = ss.substring("]");
j++;
}
Matcher m = p1.find (s);
if (m.find ()) {
String submitString1 = m.group (1);
}
随手瞎写的,作伪代码吧,具体的记不大清了.不过取第一个字符串的正则式应该是对的.第二个正则式有些小复杂,请高手接着写.
<([^>])>
List l = new ArrayList();
while(m.find()){
l.put(m.group());
}
System.out.println(l);
可以取出<>里的内容
取$1的值
String[] s2;//提取[]里的内容
String ss = s;
int i,j;
while(s.indexOf("<") != -1 ){
s1[i] = new String(s.substring(s.indexOf("<")+1,s.indexOf(">")));
s = s.substring(">");
i++;
}
while(ss.indexOf("[") != -1 ){
s2[j] = new String(ss.substring(ss.indexOf("[")+1,ss.indexOf("]")));
ss = ss.substring("]");
j++;
}楼上的兄弟这个程序需要改一下,不然取出来的就带符号了。
import java.util.regex.Pattern;
public class Test { /**
* @param args
*/
public static void main(String[] args) {
String src = "<马到成功> [c:/1.jpg] <长城> [c:/2.jpg]";
String regex = "<([^>]*)>[\\s]*\\[([^]]*)\\]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(src);
while(m.find())
{
System.out.println(m.group(1) + ":" + m.group(2));
}
}}