[Solved] Java regex accepting only email with .ca domain [closed]


in the spirit of teaching you how to fish… these are all directly relevant to problems with your attempt and might help you improve.

  • one trend i see in your regex code is that you are escaping . to make it literal by using \\.. By double-escaping it you are saying that you want a literal slash followed by a single character wildcard. So that should be \. I see you doing this for other special characters too.
  • it may be easier for you to write these special characters in class format like this [.]. that means literal dot as well and doesn’t use any escaping.
  • you don’t need to escape special characters in a character class. I think you did this for \\+
  • you can write [A-Za-z] simpler as [A-z]
  • [0-9] could be written as [\d]
  • [_A-z0-9] coudl be written as [\w]
  • You’re missing a closing )

To get you going immediately this should work:

([\w-\.]+)@((?:[\w]+\.)+)(ca)

solved Java regex accepting only email with .ca domain [closed]