日期:2014-05-20  浏览次数:20822 次

怎么用正则来提取内容?马上给分
怎么用正则来提取内容?马上给分

String   str   =   “ffafd&#test1;afafdi&#test2;&#test3;1344343....”

怎么把test1   ,test2,test3   (就是&#和;之间的内容?)给提取出来,然后放到数组里面啊?


------解决方案--------------------
up
------解决方案--------------------
String str = "ffafd&#test1;afafdi&#test2;&#test3;1344343.... ";
String patternStr = "#([^&;]*?); ";
// Compile regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
// Get the match result
String replaceStr = matcher.group();
// this is what you want
replaceStr = replaceStr.replaceAll(patternStr, "$1 ");
// for test
System.out.println(replaceStr);
}
------解决方案--------------------
public class RegTest
{
/**
* @param args
*/
public static void main(String[] args)
{
java.util.regex.Pattern p=java.util.regex.Pattern.compile( "&#([^&]*); ");
java.util.regex.Matcher m=p.matcher( "ffafd&#test1;afafdi&#test2;&#test3;1344343 ");
java.util.List list=new java.util.ArrayList();
while(m.find())
{
System.out.println(m.group(1));
list.add(m.group(1));
}
Object[] s=list.toArray();
for(int i=0;i <s.length;i++)
System.out.println(s[i]);
}
}
------解决方案--------------------
String str = "ffafd&#test1;afafdi&#test2;&#test3;1344343.... ";
Pattern p=Pattern.compile( "&#[^&#;]+; ");
Matcher m=p.matcher(str);
while(m.find()) System.out.println(m.group().replaceAll( "[&#;] ", " "));