合并两个正则表达式 [英] merge two regular expressions

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

问题描述

我有两个正则表达式,一个从csv字符串中取出用户名,另一个用来取出电子邮件。

I have two regular expressions, one pulling out usernames from a csv string, and the other pulling out emails.

字符串格式如下:

String s = "name lastname (username) <mail@mail.something.dk>; name lastname (username) <mail@mail.something.dk>; name lastname (username) <mail@mail.something.dk>";

我的正则表达式的代码是这样的。

the code for my regular expressions are like this.

Pattern pattern = Pattern.compile("(?<=\\()[^\\)]+");
Matcher matcher = pattern.matcher(s);
Pattern pattern2 = Pattern.compile("((?<=<)[^>]+)");
Matcher matcher2 = pattern2.matcher(s);

while (matcher.find() && matcher2.find()) {
    System.out.println(matcher.group() + " " + matcher2.group());
}

我发现了几个关于合并正则表达式的问题,但是从我的答案中我找到了我能够弄清楚如何合并我的。

I've found several qeustions about merging regexes, but from the answers i haven't been able to figure out how to merge mine.

我的打印输出显示:

"username mail@mail.com"

我可以使用一个正则表达式从单个匹配器打印出相同的

would I be able to print out the same from a single matcher, using one regex?

obs:这是一个学校作业,这意味着我不要需要合并它们或做更多,但我想知道它是否可能,以及它有多难。

obs: this is a school assignment, which means i do not "need" to merge them or do any more, but i'd like to know if it is possible, and how difficult it would be.

推荐答案

你可以在你的多个正则表达式之间使用 Pipe(|)来匹配所有这些: -

You can just use an Pipe (|) in between your multiple Regex, to match all of them : -

    String s = "name lastname (username) <mail@mail.something.dk>; name lastname
            (username) <mail@mail.something.dk>; name lastname 
            (username) <mail@mail.something.dk>;";

    // Matches (?<=\\()[^\\)]+  or  ((?<=<)[^>]+)
    Pattern pattern = Pattern.compile("(?<=\\()[^\\)]+|((?<=<)[^>]+)");
    Matcher matcher = pattern.matcher(s);

    while (matcher.find()) {
        System.out.println(matcher.group());
    }

输出: -

username
mail@mail.something.dk
username
mail@mail.something.dk
username
mail@mail.something.dk

UPDATE : -

如果您想打印用户名电子邮件只有当它们都存在时,你需要在; 上拆分你的字符串,然后在每个字符串上应用下面的Regex。

If you want to print username and email only when they both exists, then you need to split your string on ; and then apply the below Regex on each of them.

以下是代码: -

    String s = "name lastname (username) ; 
                name lastname (username) <mail@mail.something.dk>; 
                name lastname (username) <mail@mail.something.dk>;";

    String [] strArr = s.split(";");

    for (String str: strArr) {

        Pattern pattern = Pattern.compile("\\(([^\\)]+)(?:\\))\\s(?:\\<)((?<=<)[^>]+)");
        Matcher matcher = pattern.matcher(str);

        while (matcher.find()) {
            System.out.print(matcher.group(1) + " " + matcher.group(2));
        }
        System.out.println();
    }

输出: -

username mail@mail.something.dk
username mail@mail.something.dk // Only the last two have both username and email

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

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