-3

I am trying to match a regex from an email. If the e-mail says "need update on SRT1000" the regex needs to match. I have my code as below, but it is not working. Can someone look at this and let me know what is wrong here?

def status_update_regex(email):
    email = email.lower()
    need_sr_update_regex = re.compile('(looking|want|need|seek|seeking|request|requesting)([^/./!/?/,/"]{0,10})(status|update)(^.{0,6})(^srt[0-9]{4})')
    if need_sr_update_regex.search(email) != None:
        return 1
    else:
        return 0
user3437212
  • 637
  • 1
  • 10
  • 20
  • 1
    Trivially, a regex that matches "need update on SRT1000" is "need update on SRT1000". As you obviously need it to match more than that, it might help to describe what each part of your regex is supposed to do, because I'm having a hard time following some parts of it. – glibdud Oct 09 '19 at 19:54
  • 1
    Do you possibly need to capitalize `srt` in your regular expression? – Matt Cremeens Oct 09 '19 at 19:55
  • 4
    Your regular expression has numerous problems - You should do some match testing using a regex site like [regex101](https://regex101.com/) to make sure you're properly crafting up your expression. – b_c Oct 09 '19 at 19:55
  • 2
    Just a tip: move your ```re.compile``` outside the function. There is no sense in compile the expression everytime you call ```status_update_regex()```. – accdias Oct 09 '19 at 19:56
  • 1
    `(^.{0,6})` looks problematic. You can't be matching the start of string at this point. – Booboo Oct 09 '19 at 19:58

2 Answers2

1

Don't put the ^ in the groups, it's trying to match the beginning. Also the extra / are unnecessary.

'(looking|want|need|seek|seeking|request|requesting)([^/.!?,"]{0,10}(status|update)(.{0,6})(srt[0-9]{4})' 
jpkotta
  • 9,237
  • 3
  • 29
  • 34
  • As the first character inside of square brackets, the caret means "do not match" the following characters. See [Carets in Regular Expressions](https://stackoverflow.com/questions/16944357/carets-in-regular-expressions#16944517) – accdias Oct 09 '19 at 20:22
  • I know, I clearly kept that. OP also had `^` in the parenthesized groups. – jpkotta Oct 09 '19 at 22:10
  • My comment was meant to explain that to the OP. Sorry for the confusion. :-) – accdias Oct 09 '19 at 22:58
1
  • You didn't put whitespace \s between words.
  • You don't have the on string

(looking|want|need|seek|seeking|request|requesting)([\s^.!?,"]{0,10})(status|update)([\s^.]{0,6})(on)([\s^.]{0,6})(srt[0-9]{4})

The best tip I can give to anyone attempting regex matching is to test their solution using https://rubular.com/

Ian
  • 3,605
  • 4
  • 31
  • 66