1

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


    }
BABURAJ VD
  • 69
  • 2
  • 11
  • Which language are you using? Please add a tag of that language. – JRodDynamite Oct 28 '15 at 07:00
  • 1
    The best way will be to grab this whole text as string, split by space for array and loop in array to check if particular array value contains (at). If it's necessary i can also Replace the (at) words to @ char and (dot) for . in the same loop. Your function can return you a string with all email addresses or for example List of all addresses - it's up to you. – Arsey Oct 28 '15 at 07:18
  • @JasonEstibeiro Jason, I'm using Java language – BABURAJ VD Oct 28 '15 at 07:52
  • @Arsey I Just copied the entire string manually to an excel cell and there I tried replace (at) with @.But if failed and it didnt replaced.So will it produce the same result while scripting as per your suggestion? – BABURAJ VD Oct 28 '15 at 08:00
  • 1
    I don't understand why you can't use Regex here? insteam of `@` you got `(at)` and instead of `.` you got `(dot)`. – LiranBo Oct 28 '15 at 08:05
  • @LiranBo Liran, I think you clearly understood my query.So could you please help me by sharing a sample script for this particular function.I mean to separate the emails from this paragraph? – BABURAJ VD Oct 28 '15 at 08:13
  • I can do a regex if you want – LiranBo Oct 28 '15 at 08:14
  • @LiranBo Sure,Thanks in advance :) – BABURAJ VD Oct 28 '15 at 08:15

2 Answers2

2

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]);
}
StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
1

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.

Community
  • 1
  • 1
LiranBo
  • 2,054
  • 2
  • 23
  • 39
  • I have tried but emails are not getting fetched. Submitting the code for the same.Please look into the code , that I have tried – BABURAJ VD Oct 28 '15 at 09:14