Java获得正则表达式的匹配组 [英] Java get matches group of Regex

查看:462
本文介绍了Java获得正则表达式的匹配组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下Java表达式代码:

Given following Java expression codes:

boolean match = row.matches("('.*')?,('.*')?");

如果匹配 true ,这意味着正则表达式匹配整个'行'。然后我可以获得两组的内容吗?每一个都是('。*')

if the match is true, it means the regex matches whole 'row'. Then I could I get the content of the two groups? Each one is ('.*')?

推荐答案

访问您需要使用的组匹配器 Pattern.compile(正则表达式).matcher(行)

To access the groups you need to use Matcher: Pattern.compile(regex).matcher(row).

然后你可以打电话给 find()匹配()匹配器执行匹配器,如果它们返回true,您可以通过 group(1) group(2)。

Then you can call find() or matches() on the matcher to execute the matcher and if they return true you can access the groups via group(1) and group(2).

String row = "'what','ever'";
Matcher matcher = Pattern.compile("('.*')?,('.*')?").matcher( row );
if( matcher.matches() )
{
  String group1 = matcher.group( 1 );
  String group2 = matcher.group( 2 );
}

这篇关于Java获得正则表达式的匹配组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆