0

I tried the following regular expression to deny the user from entering email ID such as [email protected]

    @"^((?!\1{2,})([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))(?!\1{2,})@((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])(?!\1{2,})\.
([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])(?!\1{2,})\.
([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])(?!\1{2,})\.
(?!\1{2,})([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|([a-zA-Z]+[\w-]+\.)
(?!\1{2,})+(?!\1{2,})[a-zA-Z]{2,4})$"

But It didn't work. Can anyone help

Edit: 1. Not more than twice can a character appear.

RandomUser
  • 1,843
  • 8
  • 33
  • 65

2 Answers2

0

(\w)\1\1 should do the trick.

The \w matches any word character and puts it in the first capture group, then \1 checks for that same capture group, so this will match any string with three instances of a word character in a row. So any email which does NOT match this regex would be valid by your criteria.

As others have said, doing this validation is probably a bad idea, but if this is a requirement you've been given and can't avoid, the above solution will work. Alternatively you could use this to present the user with a warning, rather than reject the email altogether.

Ben Aaronson
  • 6,955
  • 2
  • 23
  • 38
  • 1
    Related: http://www.iiiiiiii.com/ (this isn't a counter example, just some music) – Kobi May 08 '14 at 12:04
  • 1
    @Kobi matches the pattern, as it should. Hopefully it's relatively clear from the last paragraph of my answer that this isn't an endorsement of blocking emails based on this pattern. But sometimes developers are given requirements that they have to implement whether or not they agree with them. And I could perhaps see value in it just placing an unobtrusive "Warning: this email contains repeated characters, please check that it is correct" next to the entry field- though it's not something I'd choose to do myself. – Ben Aaronson May 08 '14 at 12:04
  • "(this isn't a counter example, just some music)" Ah :) – Ben Aaronson May 08 '14 at 12:08
0

Bellow should work

^((.)\2{0,1}(?!\2))*$

st4hoo
  • 2,196
  • 17
  • 25