Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge two regular expressions

Tags:

java

regex

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

the string format is like this:

String s = "name lastname (username) <[email protected]>; name lastname (username) <[email protected]>; name lastname (username) <[email protected]>";

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.

my printouts show:

"username [email protected]"

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

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.

like image 888
KristianMedK Avatar asked Oct 12 '12 11:10

KristianMedK


People also ask

Why can't I combine 4 + 1 in a regex?

Combining the regex for the fourth option with any of the others doesn't work within one regex. 4 + 1 would mean either the string starts with @ or doesn't contain @ at all. You're going to need two separate comparisons to do that. Thanks for contributing an answer to Stack Overflow!

What is the concatenation of regex?

The concatenation of Regex in the programming world can be understood as combining text patterns to obtain a new text pattern, such as “Hello” + “World” is /HelloWorld/. Whenever RegExp () is called, it creates a new RegExp object. Example 1: This example creating an expression without actually using the Regex literal syntax.

What happens when REGEXP () is called?

Whenever RegExp () is called, it creates a new RegExp object. Example 1: This example creating an expression without actually using the Regex literal syntax. This allows you to make arbitrary string manipulation before it becomes a Regex object.

How to obtain a new text pattern using regexp ()?

to obtain a new text pattern, such as “Hello” + “World” is /HelloWorld/. Whenever RegExp () is called, it creates a new RegExp object. Example 1: This example creating an expression without actually using the Regex literal syntax. This allows you to make arbitrary string manipulation before it becomes a Regex object.


1 Answers

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

    String s = "name lastname (username) <[email protected]>; name lastname
            (username) <[email protected]>; name lastname 
            (username) <[email protected]>;";

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

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

OUTPUT: -

username
[email protected]
username
[email protected]
username
[email protected]

UPDATE: -

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.

Here's the code: -

    String s = "name lastname (username) ; 
                name lastname (username) <[email protected]>; 
                name lastname (username) <[email protected]>;";

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

OUTPUT: -

username [email protected]
username [email protected] // Only the last two have both username and email
like image 97
Rohit Jain Avatar answered Nov 15 '22 14:11

Rohit Jain