-2

I am trying to make a registration form in Java that accepts a string in the form of: ok@ok.ok, right now, I have -

(email.matches("[^a-zA-Z0-9]+")

which I need it to also accept @, _,-,. symbols but am unsure how to add it into the regex shown earlier.

As well as this I am aware that there is a way to separate to accept an amount of characters, and then read a specific symbol, (in my case it would be "@" and "." for the email) but am unable to implement it, something along the lines of

(email.matches("[^a-zA-Z0-9]{1-99}[1][@][^a-zA-Z0-9]{1-99}[1][.][^a-zA-Z0-9]+")

Thanks

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189

1 Answers1

0

You could try the following:

[^@]+@[^.]+\\..+

Explanation:

[^@]+ - match everything up until the @ character
@     - match the @ character
[^.]+ - match everything up until the . character
\\.   - match the . character
.+    - match the rest

In case you would like to access the different parts of the email address, i.e., what was provided before and after the @ sign, you can additionally wrap those parts of the expression in parenthesis (search for "groups" in the context of regular expressions to learn more about that).

Thomas
  • 17,016
  • 4
  • 46
  • 70