Java 支持模式中的“(?<name>pattern)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11161149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:01:47  来源:igfitidea点击:

Java support for "(?<name>pattern)" in patterns

javaregexmatcher

提问by aelstonjones

I was wondering if Java had an equivalent to C#'s named pattern matching. For example, in C# I can do something like this:

我想知道 Java 是否与 C# 的命名模式匹配等效。例如,在 C# 中,我可以执行以下操作:

var pattern = @";(?<foo>\d{6});(?<bar>\d{6});";
var regex = new Regex(pattern , RegexOptions.None);
var match = regex.Match(";123456;123456;");

var foo = match.Groups["foo"].Success ? match.Groups["foo"].Value : null;
var bar = match.Groups["bar"].Success ? match.Groups["bar"].Value : null;

This just seems like a clean way to grab groups. Can Java do something similar, or do I need to grab groups based on index position?

这似乎是一种吸引群体的干净方式。Java 可以做类似的事情,还是我需要根据索引位置抓取组?

String foo = matcher.group(0);

回答by matts

This is supported starting in Java 7. Your C# code can be translated to something like this:

这是从Java 7开始支持的。你的 C# 代码可以翻译成这样:

String pattern = ";(?<foo>\d{6});(?<bar>\d{6});";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(";123456;123456;");
boolean success = matcher.find();

String foo = success ? matcher.group("foo") : null;
String bar = success ? matcher.group("bar") : null;

You have to create a Matcherobject which doesn't actually perform the regex test until you call find().

您必须创建一个Matcher对象,该对象在您调用find().

(I used find()because it can find a match anywhere in the input string, like the Regex.Match()method. The .matches()method only returns true if the regex matches the entire input string.)

(我使用find()它是因为它可以在输入字符串的任何位置找到匹配项,就像Regex.Match()方法一样。.matches()如果正则表达式匹配整个输入字符串,该方法只返回 true。)

回答by tchrist

Java v1.7 now supports Perl-standard named groups like (?<name>...)and \k<name>in patterns.

Java v1.7 现在支持 Perl 标准命名组 like(?<name>...)\k<name>in patterns。

You cannot have duplicate group names in the same pattern, which can be annoying in very complex cases where you are building up larger patterns out of smaller pieces out of your control. It also lacks relative indexing.

同一模式中不能有重复的组名,这在非常复杂的情况下可能会很烦人,因为您要从无法控制的较小部分构建较大的模式。它还缺乏相对索引。

However, it should be enough for such simple things as you appear to be writing.

但是,对于您似乎正在编写的这些简单的事情,它应该足够了。

回答by owen gerig

I believe you need import

我相信你需要进口

org.apache.commons.lang3.StringUtils;

org.apache.commons.lang3.StringUtils;

for this

为了这

 private Boolean validateEmail(String email)
    {
        return email.matches("^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$");
    }

    private Boolean validateIP(String IP)
    {
        return IP.matches("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
    }

    private Boolean validateHostname(String Hostname)
    {
        return Hostname.matches("^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$");
    }