I have a regular expression to validate an email and when I run it, it constantly tells me that its an invalid email address but the email is correct
email = (EditText) findViewById(R.id.email);
final String Email = email.getText().toString();
if (!Email.matches("^([a-zA-Z0-9_\\-\\.]+)#([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$"))
{
email.requestFocus();
email.setError("INVALID EMAIL ADDRESS");
}
Does anyone know it would give me the error even though it is correct?
try this instead. this should remove any hidden spaces/characters after the email.
Email.matches
("([a-zA-Z0-9_\\-\\.]+)#([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}).*"))
according to http://developer.android.com/reference/java/lang/String.html
public boolean matches (String regularExpression)
Added in API level 1 Tests whether this string matches the given
regularExpression. This method returns true only if the regular
expression matches the entire input string. A common mistake is to
assume that this method behaves like contains(CharSequence); if you
want to match anywhere within the input string, you need to add .* to
the beginning and end of your regular expression. See matches(String,
CharSequence).
Related
I tried to make security to display email data by replacing some words with symbol (*) but not as expected there might be an error in making the example script as below.
String email = "thismyemail#myhost.com";
String get_text = email.get_text(3, 6);
String hasil = email.replace(get_text,"*");
email_string = (EditText) findViewById(R.id.emailT);
email_string.setText(hasil);
But the result is like this
thi*email#myhost.com
Which I expect
thi***email#myhost.com
String hasil = email.replace(get_text,"***");
But please note that if that text appears anywhere else in the string it will be replaced as well.
Also, if the email is like jf#mymailserver.com you won't be replacing a part of their user id with *.
So you can probably find a better way to select the characters, taking into account email length and also not "replacing" text but rather putting those chars at the specific position you want to.
See this related question for some ideas on how to improve this:
masking of email address in java
Your code seems right. If ur expected output is like the one mentioned above, you can just add 2 more "*" to the code.
String hasil = email.replace(get_text,"***");
I hope it helps
Is there any regex to validate email which disallow .com.com i.e example#example.com.com but allow .com.uk?
I have pasted my code that should return false while user enters .com.com. Can anyone verify my code below?
String ePattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern p =Pattern.compile(ePattern);
Matcher m = p.matcher(studentemail);
b= m.matches();
if(!b) {
eMsg.addError("email must be in formate: example#example.xxx / example#example.xx");
request.setAttribute("errorMessage", eMsg);
requestDispatcher = request.getRequestDispatcher("/addrecord.jsp");
requestDispatcher.forward(request, response);
return;
}
else {
System.out.println("valid email id");
}
Such a regex isn't limited to Java in most cases (only if special capabilities are used that are not supported by the Java regex engine). Besides that, if you want to match #example.xx only you should adjust the second part of your regex (especially the first group in that part). But note that there are domains like xxx.co.uk which might not be valid then.
Also xx.com.com might be an invalid domain but it also might not (at least in other combinations, e.g. de.de is a valid domain) so you might want to either allow that, test for specific combinations only or restrict usage of such domains.
However, depending on your use-case it would probably be better to send a confirmation email since a regex can just tell you that it looks correct but there might still be errors.
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.
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.
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. I have prepared a snippet that finds email ids from the given chunk of text.
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.
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).
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:
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.
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??
What is the best way to check the cc field in an email for correct addresses?
For example if I have cc: mail#mail.com;me#gmail.com;asdfj
How can I detect the asdfj as an invalid address?
I'm using java. The best way I can think of is using a stringtokenizer but I'm not sure how to implement this.
The code I had earlier checks the cc field but only for one email address using this:
if( (!Cc.equals("")) && ccat != Cc.lastIndexOf('#')) {
System.out.println("CC: address is invalid 2");
return false;
}
Thanks.
I'd use JavaMail's InternetAddress class. It has many methods to play with and you won't need a regular expression.
You should split the CC field on the semicolon using the String split function and then use a regex email validation pattern on each address.
I would like a regular expression that will extract email addresses from a String (using Java regular expressions).
That really works.
Here's the regular expression that really works.
I've spent an hour surfing on the web and testing different approaches,
and most of them didn't work although Google top-ranked those pages.
I want to share with you a working regular expression:
[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})
Here's the original link:
http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/
I had to add some dashes to allow for them. So a final result in Javanese:
final String MAIL_REGEX = "([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})";
Install this regex tester plugin into eclipse, and you'd have whale of a time testing regex
http://brosinski.com/regex/.
Points to note:
In the plugin, use only one backslash for character escape. But when you transcribe the regex into a Java/C# string you would have to double them as you would be performing two escapes, first escaping the backslash from Java/C# string mechanism, and then second for the actual regex character escape mechanism.
Surround the sections of the regex whose text you wish to capture with round brackets/ellipses. Then, you could use the group functions in Java or C# regex to find out the values of those sections.
([_A-Za-z0-9-]+)(\.[_A-Za-z0-9-]+)#([A-Za-z0-9]+)(\.[A-Za-z0-9]+)
For example, using the above regex, the following string
abc.efg#asdf.cde
yields
start=0, end=16
Group(0) = abc.efg#asdf.cde
Group(1) = abc
Group(2) = .efg
Group(3) = asdf
Group(4) = .cde
Group 0 is always the capture of whole string matched.
If you do not enclose any section with ellipses, you would only be able to detect a match but not be able to capture the text.
It might be less confusing to create a few regex than one long catch-all regex, since you could programmatically test one by one, and then decide which regexes should be consolidated. Especially when you find a new email pattern that you had never considered before.
a little late but ok.
Here is what i use. Just paste it in the console of FireBug and run it. Look on the webpage for a 'Textarea' (Most likely on the bottom of the page) That will contain a , seperated list of all email address found in A tags.
var jquery = document.createElement('script');
jquery.setAttribute('src', 'http://code.jquery.com/jquery-1.10.1.min.js');
document.body.appendChild(jquery);
var list = document.createElement('textarea');
list.setAttribute('emaillist');
document.body.appendChild(list);
var lijst = "";
$("#emaillist").val("");
$("a").each(function(idx,el){
var mail = $(el).filter('[href*="#"]').attr("href");
if(mail){
lijst += mail.replace("mailto:", "")+",";
}
});
$("#emaillist").val(lijst);
The Java 's build-in email address pattern (Patterns.EMAIL_ADDRESS) works perfectly:
public static List<String> getEmails(#NonNull String input) {
List<String> emails = new ArrayList<>();
Matcher matcher = Patterns.EMAIL_ADDRESS.matcher(input);
while (matcher.find()) {
int matchStart = matcher.start(0);
int matchEnd = matcher.end(0);
emails.add(input.substring(matchStart, matchEnd));
}
return emails;
}