Regex question

Posted by Poromenos on Mon 26 Apr 2004 06:08 PM — 6 posts, 22,907 views.

Greece #0
<hp[1953/1953]>Poromenos tells you 'stats'

The "Poromenos tells you 'stats'" part is white. Is there any way i can also match everything before Poromenos, to avoid the following:
Nick tells you 'Poromenos tells you 'stats'

with the trigger "([A-Za-z]+) tells you \'stats\'$" this would match on Poromenos, and not on Nick. Is there any way to make it match on "Nick tells you 'Poromenos" instead of just Poromenos? The prompt is not white, and I need colour matching to stay.
Australia Forum Administrator #1
What you can do here is use an "assertion". This checks things but does not consume characters, so the matching text is still just what it was before so the colour matching will still be on the correct portion.

If you look at:


http://mushclient.com/pcre/pcrepattern.html#SEC15


You will see the write-up about assertions. In your case you want a look-behind assertion, because you want to make sure the text that *precedes* the matching text is, or is not, a certain thing. The limitation of look-behind assertions is that they have to be a fixed length, so you need to not match a variable thing but a fixed thing.

Two things you could do:

  1. Check that the character *before* the text "Poromenos tells you 'stats'" *is* a ">" symbol. That would exclude the case of "Nick tells you 'Poromenos tells you 'stats'", however if Nick was smart he might say "Nick tells you '>Poromenos tells you 'stats'" and defeat your test.

    To do this:


    Match: (?<=\>)([A-Za-z]+) tells you \'stats\'$


    The above is a positive assertion.
  2. Or, you could use a negative assertion. Check that the text is *not* preceded by "tells you '", like this:


    Match: (?<!tells you ')\b([A-Za-z]+) tells you \'stats\'$


    In this case I had to add the \b (assert word boundary) otherwise it matched on "oromenos tells you 'stats'" (in other words, the wildcard consumed the P and then found that "oromenos" was not preceded by "tells you '" and found a match. However the \b forces the match on whoever tells you to be a whole word and not a part word.


These assertions are pretty powerful stuff. :)
Amended on Mon 26 Apr 2004 09:32 PM by Nick Gammon
Greece #2
Ah great, I didn't know that, thanks!
Greece #3
Hm, assertions have to be fixed-length, no? So even if I use a negative assertion, someone could do:
Nick tells you '' Nick tells you 'hi'
and defeat it... Can i add a wildcard somehow?
Australia Forum Administrator #4
Why do you need the wildcard? My negative assertion picked up the words "... tells you". Isn't that enough? By omitting *who* tells you (which you don't care about) you pick up the spoof line.
Greece #5
You're right... I guess I can tell if they're spoofing or not... I'll try something and see.