java中正则匹配结果到一个String数组中

匹配类代码:

package future;


import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Regexclass {
//pattern 正则表达式,不能为空
//Sourcecode 需要正则的文本,不能为空
/*该函数功能是正则匹配出所有结果到一个String数组中*/
public static String [] GetRegResult(String pattern,String Sourcecode){
Pattern p = Pattern.compile( pattern);
   Matcher m = p.matcher( Sourcecode);
 
   ArrayList<String> tempList = new ArrayList<String>();
   while(m.find()){
    tempList.add(m.group());  
   }
   String[] res = new String[tempList.size()];  
   int i = 0;
   for(String temp : tempList){
   res[i] = temp;
    i++;
   }       
 return res;
}

}


主程序测试代码

package future;


public class Test {


public static void main(String[] args) {
// TODO 自动生成的方法存根
String str = "1云2未3归4来";
String pattern = "\\d{1,}";
String [] res= Regexclass.GetRegResult(pattern, str);
for (int i=0;i<res.length;i++){
System.out.println(res[i]);
}
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FL1623863129

你的打赏是我写文章最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值