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
The following code will extract your pairs. The regex is quite short, but I am almost sure, there is a more elegant way (there always is with regex!). ;)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String s = "name1 lastname1 (user1); name2 lastname2 (username2) <mail2@mail.something.dk>; name3 lastname3 (username3) <mail3@mail.something.dk>;";
Pattern pattern = Pattern.compile("\\(([^\\)]+)\\)\\s<([^>]+)>");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group(1) + " " + matcher.group(2));
}
}
}
Output:
username2 mail2@mail.something.dk
username3 mail3@mail.something.dk
Explanation for the regex "\\(([^\\)]+)\\)\\s<([^>]+)>"
:
\\(([^\\)]+)\\)
: A group of non-)
characters enclosed by (
and )
\\s
: A space in between<([^>]+)>
: A group of non->
characters enclosed by <
and >
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());
}
OUTPUT: -
username
mail@mail.something.dk
username
mail@mail.something.dk
username
mail@mail.something.dk
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) <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();
}
OUTPUT: -
username mail@mail.something.dk
username mail@mail.something.dk // Only the last two have both username and email