2

I am trying to check if an at character only exist in an android edit-view. I am new to regular expression and i implemented the forthcoming code. where am i mistaken ? Here is my code

try {

        Pattern pattern = Pattern.compile("/@");
        Matcher matcher = pattern.matcher(valEmail);

        if (!matcher.matches()) {

            Toast.makeText(this," Invid no @ character ",Toast.LENGTH_LONG).show();

        }

    }catch(Exception ex){}
5
  • for which do you have write Regular Expression ,i think you want for Email? Commented Apr 25, 2012 at 6:12
  • i would like to check if only one @ character present in an email address using regular expression Commented Apr 25, 2012 at 6:15
  • if you want Regular Expression for Email check my answer. Commented Apr 25, 2012 at 6:16
  • 1
    You may want to look at this site for pattern for email validation. The problem with your code is that you don't need the "/" before the "@" Commented Apr 25, 2012 at 6:16
  • Thank you for the site explanation. It helps a lot and you're right dude Commented Apr 25, 2012 at 6:26

5 Answers 5

6

There are two problems with your current code:

  • The leading slash means it really is looking for "/@" rather than just "@". You seem to think that "/" does something special within a regex - it doesn't.
  • You're using matches() which tries to match the whole of the input; you want find which will just try to find a match for the regex somewhere in the input.

Why are you using regular expressions at all though? Why not just:

if (!valEmail.contains("@")) {
    ...
}

That will check whether the address contains any "@" signs. If you want to check that there's only one "@" sign, you could use:

int atIndex = valEmail.indexOf('@');
if (atIndex == -1) {
    // Handling for *no* @ sign
}
if (valEmail.indexOf('@', atIndex + 1) != -1) {
    // Handling for multiple @ signs
}

If you do want to use regular expressions, there are rather more sophisticated email address validation regular expressions available. (There lots of different ones with different levels of validity - make sure you get one designed for the Java flavour of regex.) I wouldn't use one just for this though - only use regular expressions where you're really interested in pattern matching.

If you want to use a regex for "at least one non-@, following by @, followed by at least one non-@" you could use:

// TODO: Compile this once and reuse
Pattern pattern = Pattern.compile("[^@]+@[^@]+");
Matcher matcher = pattern.matcher(valEmail);

if (!matcher.matches()) {
    ...
}

As an aside, this:

catch(Exception ex){}

is never a good idea. Please don't just ignore errors indiscriminately.

Sign up to request clarification or add additional context in comments.

2 Comments

Your code is very short, and very incorrect. Get it right first. I also wouldn't consider regexes to be a pinnacle of performance.
@user1355385: Compiling a regular expression and then using it is not going to be faster (or shorter) than text.contains("@"). And as StilesCrisis says - get your code working first before you worry about performance.
2

For Email you can use Below Regular Expression .

 private static final String EMAIL_PATTERN = "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$";



    try {

    Pattern pattern = Pattern.compile(EMAIL_PATTERN);
    Matcher matcher = pattern.matcher(valEmail);

    if (!matcher.matches()) {

        Toast.makeText(this," Invid no @ character ",Toast.LENGTH_LONG).show();

    }

}catch(Exception ex){
}

Comments

1

matches() method checks if whole string matches to regular expression. You should use find() method instead of matches.And / character shouldn't be there.
But instead of all this you better use contains() method of string. if(valEmail.contains("@"))

Comments

1

You have a leading / in the regular expression that shouldn't be there.

Additionally, you should be using find, not matches.

2 Comments

this / intend to check if only one @ character present. Why should i remove it ?
@user1355385: Why do you think it will do that?
0

Just use @ without a / in the beginning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.