0

I am trying to get last regex match on a message broadcast by socket, but it returns blank.

>>> msg = ':morgan.freenode.net 353 MechaBot = #xshellz :MechaBot ITechGeek zubuntu whitesn JarodRo SpeedFuse st3v0 anyx danielhyuuga1 AussieKid92 JeDa Failed Guest83885 RiXtEr xryz D-Boy warsoul buggiz rawwBNC MagixZ fedai Sunborn oatgarum dune SamUt Pythonista_ +xinfo madmattco BuGy azuan DarianC stupidpioneers AnTi_MTtr JeDaYoshi|Away PaoLo- StephenS chriscollins Rashk0 morbid1 Lord255 victorix [DS]Matej EvilSoul `|` united Scrawn avira ssnova munsterman Logxen niko gorut Jactive|OFF grauwulf b0lt saapete'
>>> r = re.compile(r"(?P<host>.*?) (?P<code>.*?) (?P<name>.*?) = (?P<msg>.*?)", re.IGNORECASE)
>>> r.search(msg).groups()
(':morgan.freenode.net', '353', 'MechaBot', '')

1 Answer 1

1
(?P<host>.*?) (?P<code>.*?) (?P<name>.*?) = (?P<msg>.*)

Try this.This works.See demo.Your code use .*? whch says match as few characters as you can.So while it your previous you have used .*? <space> it matches upto first space it encounters,in the last you have no specified anythng.So it did not match anythin as it s in lazy mode.

https://regex101.com/r/aQ3zJ3/1

You can also use

(?P<host>.*?) (?P<code>.*?) (?P<name>.*?) = (?P<msg>.*?)$

which says match lazily upto end.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay that works. Could you explain why ? isn't necessary please?
@DanielHyuuga: If you make the last group non-greedy, it'll match the empty string. Regex doesn't attempt to consume all of the input string, as you probably think. As soon as the pattern matches entirely, it's over.

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.