简体   繁体   中英

How to extract an email address which is not in a regular expression format from a span text using selenium web driver?

I am working on an automation project.In that I have to fetch all the email address from the span text which will be having characters of more than 200.But the email address are not in regular expression format instead it is showing as Eg:- xxx(at)abc(dot)com aaa(at)yyy(dot)com

So How can I extract these sort of contents from a paragraph like below

aaaaaaaaa aaaaaaa aaaaaaaaaaaa aaaaaaaa aaaaaaaa xxx(at)abc(dot)com bbbbbb bbbbbbbb bbbbbbbbbbbb bbbbbbbbbbbbbbbbbbb ggggggggggggg aaa(at)yyy(dot)com ccccccccccccccc ddd ccc eeeeee fff ggggg 11111(at)22222(dot)com.

public class Foxpro_Class { 
    public static void main(String[] args)
    {
    String emailStr="aaaaaaaaa aaaaaaa aaaaaaaaaaaa aaaaaaaa aaaaaaaa xxx(at)abc(dot)com bbbbbb bbbbbbbb bbbbbbbbbbbb bbbbbbbbbbbbbbbbbbb ggggggggggggg aaa(at)yyy(dot)com ccccccccccccccc ddd ccc eeeeee fff ggggg 11111(at)22222(dot)com zzzzzz";

     validate(emailStr);



    }

    public static final Pattern VALID_EMAIL_ADDRESS_REGEX = 
            Pattern.compile("^[A-Z0-9._%+-]+(at)[A-Z0-9.-]+(dot)[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);

        public static boolean validate(String emailStr) {
                Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(emailStr);

               System.out.println(matcher.toString());


                return matcher.find();
        }


    }

In the text provided in the question, there is spaces between each words. We can use this to split the text into Array and then we can separate out the emailIds

// Find the span tag which contains text (at) and (dot) and get the text
Sting textInElement = driver.findElement(By.xpath("//span[contains(.,'(at)')][contains(.,'(dot)')]")).getText();

// Split the String based on space
String[] textRequired  = textInElement.split(" ");

// Iterate through array to print the array component containing (dot)  and (at) 
for (int i=0; i<string.length; i++) {
    if (string[i].contains("(at)")&&string[i].contains("(dot)"))
        System.out.println(string[i]);
}

this code was taken from here , I only did small changes

public static final Pattern VALID_EMAIL_ADDRESS_REGEX = 
    Pattern.compile("^[A-Z0-9._%+-]+(at)[A-Z0-9.-]+(dot)[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);

public static boolean validate(String emailStr) {
        Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(emailStr);
        return matcher.find();
}

let me know if it works. if you want more help in case it doesn't match everything you like, provide detailed examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM