0

So I have a regex that goes like:

 regex1= re.compile(r'\S+@\S+')

This works perfectly but I am trying to add a character limit so the total amount of characters have to be less than 20.

I tried re.compile(r'\S+@\S+{5,20}') but it keeps giving me an error. Seems like a simple fix, but cant see what I am doing wrong.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
James Davinport
  • 303
  • 7
  • 19
  • That syntax is not to limit the total length of the string, it can't be applied after some other repeat modifier (?, *, +). – jonrsharpe Dec 17 '18 at 19:50
  • To do it properly is not easy given all possible variants, take a look at django implementation of email validation, it uses RegEx, https://git.io/fpxnn – Dalvenjia Dec 17 '18 at 19:51

1 Answers1

2

You can't specify a greedy modifier (+) with a specific number of characters (i.e., \S+{5,20) is not a valid pattern). If you're doing this in python, I'd suggest just using the len(...) function on the string in addition to the regex to verify. For example:

if regex1.match(email) and (len(email) < 20):
    ...
Tim
  • 2,756
  • 1
  • 15
  • 31