首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >java中正则匹配结果到一个String数组中

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

作者头像
云未归来
发布2025-07-18 10:37:05
发布2025-07-18 10:37:05
930
举报

匹配类代码:

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]); } } }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-05-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档