开发者社区> 问答> 正文

Java中如何进行正则表达式查找?

已解决

Java中如何进行正则表达式查找?

展开
收起
小天使爱美 2020-04-12 22:44:42 6036 0
1 条回答
写回答
取消 提交回答
  • 采纳回答

    "查找就是在文本中寻找匹配正则表达式的子字符串,,看个例子: public static void main(String[] args) { String regex = ""\d{4}-\d{2}-\d{2}""; Pattern pattern = Pattern.compile(regex); String str = ""today is 2017-06-02, yesterday is 2017-06-01""; Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(""find "" + matcher.group() + "" position:"" + matcher.start()+""-""+matcher.end()); } } 代码寻找所有类似""2017-06-02""这种格式的日期,输出为: find 2017-06-02 position:9-19 find 2017-06-01 position:34-44 Matcher的内部记录有一个位置,起始为0, find方法从这个位置查找匹配正则表达式的子字符串,找到后,返回true,并更新这个内部位置,匹配到的子字符串信息可以通过如下方法获取: public String group():匹配到的完整子字符串 public int start():子字符串的起始位置 public int end():子字符串的结束位置"

    2020-04-12 22:46:46
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载