Pattern matching removing wiki markup tags

Rodrigo B. :

I'm trying to grab a user's email address from a field in Jira that's using wiki markup. The field content is as follows:

*User Email:* [[email protected]|mailto:[email protected]] *Tech Email:* [[email protected]|mailto:[email protected]]

I need to create a regex that will match either the email of the 'User' or the 'Tech' email, not both at the same time. I'm unable to create a regex that will match that specifically. I'm using a tool that the underlying implementation is based on Java's Pattern class and uses Matcher.find() to find matches.

Ilya Lysenko :

Try this expression to match only user emails:

(?:User\sEmail:\*\s\[)([\w\-@.]+)

Here you can see how it works: https://regexr.com/51f8s

Java example:

List<String> items = Arrays.asList(
        "*User Email:* [[email protected]|mailto:[email protected]]",
        "*Tech Email:* [[email protected]|mailto:[email protected]]"
);

Pattern pattern = Pattern.compile("(?:User\\sEmail:\\*\\s\\[)([\\w\\-@.]+)");
for (String item : items) {
    Matcher matcher = pattern.matcher(item);

    if (matcher.find()) {
        System.out.println("email: " + matcher.group(1));
    }
}

Output:

email: [email protected]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=378653&siteId=1