补充:
我是要将<1></1>和<2></2>之间的字符分解到一个字符串数组,象下面这样:Pattern p=new Pattern("<\\d+>|</\\d+>");
String[] s=p.tokenizer("<1></1><2>kkk</2>").split();得到的数组只有一个元素"kkk",我要如何才能将<1></1>间的任何东西都分解出来?也就是怎么样才能使得到的数组有两个元素(即使第一个为null)?
我是要将<1></1>和<2></2>之间的字符分解到一个字符串数组,象下面这样:Pattern p=new Pattern("<\\d+>|</\\d+>");
String[] s=p.tokenizer("<1></1><2>kkk</2>").split();得到的数组只有一个元素"kkk",我要如何才能将<1></1>间的任何东西都分解出来?也就是怎么样才能使得到的数组有两个元素(即使第一个为null)?
import java.util.regex.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class PatternTest
{
public PatternTest()
{
}
public static void main(String[] args)
{
String str = "<1></1><2>kkk</2>";
Pattern p = Pattern.compile("<\\d+>(\\w*?)</\\d+>");
Matcher m = p.matcher(str);
boolean result = m.find();
while(result)
{
if("".equals(m.group(1)))
System.out.println("null");
else
System.out.println(m.group(1));
result = m.find();
}
}
}
真是烦死了!
请各位帮忙呀!
str1="<\\d+>([^>]*)</\\d+>";
String sign=",";//这个分割标志可以自定义
String s="<1></1><2>kkk</2><3>mmm</3><4></4>";
s=s.replaceAll(str,"null"+sign);
s=s.replaceAll(str1,"$1"+sign);
System.out.println(s);
String[] ss=s.split(sign);
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);