0

It Starts with between 1 and 6 lowercase letters denoted by character class [a-z]

lowercase letter are followed by an optional underscore i, e zero or one occurrence of the _ character

The optional underscore is followed by 0 to 4 optional digits denoted by character class [0-9]

Must end with @example.com

import re
re.match('^[a-z]+([_ or number]+)mustendwith.example.com, addressToVerify)

Results:

  bob@example.com True
  bob_@example.com True
  bob_1@example.com True
  bob0_@example.com False #since afternumber _ must not come
  bob1@gmail.com False

I am very new to re in python can any one help me.

I have tried but unable to achieve

import re
re.match('^[a-z]+([_ or number]+)mustendwith.example.com, addressToVerify)
Sreekiran A R
  • 2,838
  • 2
  • 18
  • 38
sagar_dev
  • 101
  • 2
  • 10
  • 1
    This site can be handy to test regexes https://regex101.com/ - in your case, you probably need something like `^[a-z]{1,6}(_\d{1,4})?+@example\.com$` - for future questions, I would recommend trying a bit harder, because I doubt you expected your example code to actually work... – Grismar Jan 24 '19 at 05:14
  • Possible duplicate of [How to validate an email address using a regular expression?](https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression) – schlebe Jan 24 '19 at 05:58

1 Answers1

3

Based on your list of match requirements, you are looking for regex along the lines of...

^[a-z]{1,6}_?[0-9]{0,4}@example\.com$

^[a-z]{1,6} match starts with between 1 and 6 lowercase letters

_? 0 or 1 underscore

[0-9]{0,4} between 0 and 4 numbers (could also use \d instead of [0-9]

@example\.com$ ends with "example.com" (backslash escapes the . since it is a special character in regex)

benvc
  • 13,540
  • 3
  • 27
  • 50
  • I am getting invalid systax, re.match(^[a-z]{1,6}_?[0-9]{0,4}@hackerrank\.com$),"julia@example.com") – sagar_dev Jan 24 '19 at 05:49
  • @sagar_dev - you need to quote your regex and fix your parentheses: `re.match(r'^[a-z]{1,6}_?[0-9]{0,4}@hackerrank\.com$','julia@example.com')`. You still won't get a match of course since you are testing an example.com address against regex for a hackerrank.com email. – benvc Jan 24 '19 at 05:53
  • 1
    Beware: the optional group is not just `_` but the full `_\d{1,4}`. That means that you need to declare a non capturing group in your regex: `r'^[a-z]{1,6}(?:_[0-9]{0,4})?@example\.com$'` – Serge Ballesta Jan 24 '19 at 06:12