Home > database >  Regex to grab validate email address in complete XML string or normal string
Regex to grab validate email address in complete XML string or normal string

Time:01-16

Need to grab string text of email value in big XML/normal string.

Been working with Regex for it and as of now below Regex is working correctly for normal String Regex : ^[\\w!#$%&'* /=?`{|}~^-] (?:\\.[\\w!#$%&'* /=?`{|}~^-] )*@(?:[a-zA-Z0-9-] \\.) [a-zA-Z]{1,6}$

Text : [email protected]

but in case when above text is enclosed in XML tag it fails to return. <email>[email protected]</email>

I am trying to amend some change to this regex so that it will work for both of the scenarios

CodePudding user response:

You have put ^ at the beginning which means the "Start of the string", and $ at the end which means the "End of the string". Now, look at your string:

<email>[email protected]</email>

Do you think, it starts and ends with an email address?

I have removed them and also escaped the - in your regex. Here you can check the following auto-generated Java code with the updated regex.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        final String regex = "[\\w!#$%&amp;'* /=?`\\{|\\}~^\\-] (?:\\\\.[\\w!#$%&amp;'* /=?`\\{|\\}~^\\-] )*@(?:[a-zA-Z0-9-] \\.) [a-zA-Z]{1,6}";
        final String string = "[email protected]\n"
       "<email>[email protected]</email>";
        
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);
        
        while (matcher.find()) {
            System.out.println("Full match: "   matcher.group(0));
            
            for (int i = 1; i <= matcher.groupCount(); i  ) {
                System.out.println("Group "   i   ": "   matcher.group(i));
            }
        }
    }
}

Output:

Full match: [email protected]
Full match: [email protected]
  • Related