0

Hello all and sorry to bother with another regex question... This time the problem I have is that I'm unable to match or exclude some strings out of a regexp:

Strings to compare against:

EVENT DATA
EVENT
EVENT SEC-1193 10222
EVENT META

I want this regex to match only EVENT SEC-1193 10222 and it's like this:

EVENT\s[\w'-]*\s[\w'-]*

The problem is that it matches everything... Any help in the reghell would be highly appreciated

4
  • can you be more specific about your matching criteria? Why don't you just match the string exactly, if that's all you need to do? Commented Dec 5, 2013 at 19:31
  • 1
    * means zero or more ...this reminds me of that regex quote... now you have 2 problems ... Commented Dec 5, 2013 at 19:32
  • Second Zoran's comment above...the * operand returns all the string with 0 or more occurrences of EVENT\s[\w'-]*\s[\w'-]. That's why it matches everything Commented Dec 5, 2013 at 19:40
  • My brain short-circuits when dealing with regexp stuff... Commented Dec 5, 2013 at 19:42

2 Answers 2

2

Use + instead of *:

>>> r = re.compile(r"EVENT\s[\w'-]+\s[\w'-]+")
>>> r.search("EVENT DATA")
>>> r.search("EVENT")
>>> r.search("EVENT SEC-1193 10222")
<_sre.SRE_Match object at 0x8e04100>
>>> r.search("EVENT META")
Sign up to request clarification or add additional context in comments.

Comments

1
EVENT\s[\w'-]+\s[0-9]+

Seems to be a bit better. Try testing your regex's with http://gskinner.com/RegExr/ or http://regex101.com/

2 Comments

Indeed looks better :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.