-5

i'm really really bad with regexes. can anyone help me build a java regex that accept only email finishing with .ca.

i've tried something like

^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*
      @[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.ca$;

Thanks

Bob J
  • 325
  • 4
  • 13

2 Answers2

1

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)
gillyspy
  • 1,578
  • 8
  • 14
0

You can try this out:

([\w-\.]+)@((?:[\w]+\.)+)(ca)
Santosh Panda
  • 7,235
  • 8
  • 43
  • 56