简体   繁体   English

检查Java文本中的电子邮件ID

[英]Check for email id in a text in java

i am using java to write Appium test script & now i want to compare two emails, but before comparison i have to fetch the email id from a text by splitting the string. 我正在使用Java编写Appium测试脚本,现在我想比较两封电子邮件,但是在进行比较之前,我必须通过拆分字符串从文本中获取电子邮件ID。 Ex: i have text like this in my application "your account email associated with pankaj@gmail.com" so i want split & capture this email id only from this text & compare it with other email id which is showing in a text box. 例如:我在我的应用程序“您的与pankaj@gmail.com相关联的帐户电子邮件”中有这样的文本,因此我只希望从此文本中分割并捕获此电子邮件ID,并将其与文本框中显示的其他电子邮件ID进行比较。 how can i do this ?? 我怎样才能做到这一点 ?? Currently i am doing it like this: 目前我正在这样做:

WebElement email_id= driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]"));

String edit_email=email_id.getText();

System.out.println(edit_email);

But getting the Full text.How can i split it. 但是获取全文。我如何拆分它。

You should try regular expression using java.util.regex.Pattern and java.util.regex.Matcher . 您应该使用java.util.regex.Patternjava.util.regex.Matcher尝试正则表达式。 I have prepared a snippet that finds email ids from the given chunk of text. 我准备了一个片段,该片段从给定的文本块中查找电子邮件ID。

    String text = "your account email associated with pankaj@gmail.com and he has emailed someone@gmail.com.";
    Pattern pattern = Pattern.compile("[\\w]+[\\d\\w]*(@)[\\w]+[\\w\\d]*(\\.)[\\w]+");
    Matcher matcher = pattern.matcher(text);
    while(matcher.find()){
        System.out.println(matcher.group());
    }

This should help. 这应该有所帮助。

This is doing the trick for me: 这对我来说是窍门:

    String s = "your account email associated with pankaj@gmail.com";
    s = s.replaceAll("^.+\\s", "");
    System.out.println(s);

If you are sure that the text that you are attempting to split is of standard format (or some static content with different email Ids), you can use regular expressions to parse and retrieve the email addresses as Nitheesh Shah and dotvav mentioned in their answers. 如果您确定要拆分的文本为标准格式(或某些具有不同电子邮件ID的静态内容),则可以使用正则表达式来解析和检索其答案中提到的电子邮件地址,如Nitheesh Shah和dotvav。

Otherwise, you have to follow couple of RFCs as mentioned in the below thread to perfectly retrieve and validate the email addresses (Refer the best answer which is shown at the top of the below thread). 否则,您必须遵循下面的线程中提到的RFC,才能完美地检索和验证电子邮件地址(请参阅下面的线程顶部显示的最佳答案)。

Using a regular expression to validate an email address 使用正则表达式验证电子邮件地址

As OP has mentioned it will end with email id only , another solution can be this: 如OP所述,它将仅以电子邮件ID结尾,另一种解决方案可以是:

WebElement email_id= driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]"));
String s[] = email_id.getText().split(" ");
System.out.println(s[s.length-1]);

After getting email id you can compare it with another email in textbox. 获取电子邮件ID后,您可以将其与文本框中的其他电子邮件进行比较。

Currently I am using this & it's working for me perfectly.implemented the solution as finding the particular email substring in the main string. 目前,我正在使用此功能,并且对我来说效果很好。实施该解决方案的方法是在主字符串中找到特定的电子邮件子字符串。

String word = edit_email;

String com_txt= email_text; //Edit page Static string

Boolean same_txt =com_txt.contains(word);

Boolean result=same_txt;

if(result==true)

    {

System.out.println(result);

System.out.println("Edit screen & enter email screen contains the              same email");


    }

Is this the right way to perform the comparison?? 这是进行比较的正确方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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