Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ General ➜ Regexp help needed

Regexp help needed

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Thu 12 May 2005 10:56 AM (UTC)
Message
I can't seem to come up with a regexp for a text. I'm sure it's doable, but I'm just inept. ;) The text is:
Quote:
You are hit by a indigo and orange ray.

It can vary ofcourse. There are seven possible colours, and how many hits you are random. You could get:
Quote:
You are hit by a orange ray.

Or the full:
Quote:
You are hit by a indigo, yellow, green, violet, blue, red and orange ray.

Doing multiple triggers for 1, 2, 3, etc colors would work, but it's not "right". :P So.. How could I do this?
I would want to run checks on the colors in a script later on, as each color represents an effect (affliction, damage, heal).

Simplicity is Divine | http://nogfx.org/
Top

Posted by Tsunami   USA  (204 posts)  Bio
Date Reply #1 on Thu 12 May 2005 01:00 PM (UTC)
Message
The easiest way to do it:

^You are hit by a (.+) ray\.$

Then you can use the InStr function (VBScript), or a similar function to match on different colors in wildcs(1).

This next is also a possibility, although I wouldn't think it's any faster than the above, just more 'elegant'. There are essentially five parts:

1) '^You are hit by a'
2) '( \w+?\,)'
3) '(\w+?)'
4) 'and (\w+?)'
5) ' ray\.$'

^You are hit by a(( \w+?\,)* (\w+?)? (and (\w+?) )?) ray\.$

hope I haven't forgotten any parens there!
Top

Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Reply #2 on Thu 12 May 2005 03:16 PM (UTC)

Amended on Thu 12 May 2005 03:17 PM (UTC) by tobiassjosten

Message
Didn't work. I changed it some, and got a match. But it's not capturing all the words.
Trigger: ^You are hit by a( (\w+)(\,| and))* (\w+) ray\.$
for color in pairs(wildcards) do
    Note (wildcards[color])
end

Output:
(test) You are hit by a white and orange ray.
 white and
white
 and
orange
You are hit by a white and orange ray.
(test) You are hit by a white, blue and orange ray.
 blue and
blue
 and
orange
You are hit by a white, blue and orange ray.

Simplicity is Divine | http://nogfx.org/
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #3 on Thu 12 May 2005 07:21 PM (UTC)

Amended on Thu 12 May 2005 07:23 PM (UTC) by Ked

Message
Being overly explicit never hurts:


^You are hit by a (indigo|)(?:, | and | |)(yellow|)(?:, | and | |)(green|)(?:, | and | |)(violet|)(?:, | and | |)(blue|)(?:, | and | |)(red|)(?:, | and | |)(orange)(?: |)ray\.$


It'll match only if the colours always appear in the same order.
Top

Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Reply #4 on Thu 12 May 2005 08:19 PM (UTC)

Amended on Thu 12 May 2005 08:22 PM (UTC) by tobiassjosten

Message
Sweet. Not sure if they show up in the same order, and I currently can't find anyone to demonray me.. But, until I do, I could do like this:
^You are hit by a (\w+|)(?:, | and | |)(\w+|)(?:, | and | |)(\w+|)(?:, | and | |)(\w+|)(?:, | and | |)(\w+|)(?:, | and | |)(\w+|)(?:, | and | |)(\w+)(?: |)ray\.$

..right?

Ohh. The ?: keeps it from being sent as a wildcard, right?

Edit: For some wierd reason, I got this output:
(test) You are hit by a indigo and orange ray.
indigo
orang




e
You are hit by a indigo and orange ray.
(test) You are hit by a indigo ray.
indig





o
You are hit by a indigo ray.
(test) You are hit by a indigo, blue and green ray.
indigo
blue
whit



e
You are hit by a indigo, blue and white ray.

It strips the last letter of the last word.. Why?

Simplicity is Divine | http://nogfx.org/
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #5 on Thu 12 May 2005 08:24 PM (UTC)

Amended on Thu 12 May 2005 08:28 PM (UTC) by Ked

Message
Yes and yes. Though I personally try to avoid using \w+, since it is almost always exceptionally slow. So I'd just replace \w+ with a spammy but explicit and fast:

green|yellow|violet|red|indigo|blue|orange

EDIT: That's another reason why I loath the \w+ - you can never predict exactly what it is going to capture. I once tested it on a very long sequence of "testtesttesttesttesttesttest...", as part of a discussion about Zmud's internal trigger match syntax vs standard regexp one, and (\w+) would yield all sorts of completely absurd captures.
Top

Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Reply #6 on Thu 12 May 2005 08:36 PM (UTC)
Message
Doesn't match..? Wierdo. Any more suggestions? I was thinking, maybe you could utilise the *-char a little more, instead of making "slots" for all the colors? Can't you? Still pretty inept with regexp, so bare with me. ;)

Furthermore, \w+ is much slower? I switched recently to MC from zMUD because of its speed, and so far most of my triggers uses \w+..

Simplicity is Divine | http://nogfx.org/
Top

Posted by Larkin   (278 posts)  Bio
Date Reply #7 on Thu 12 May 2005 08:43 PM (UTC)
Message
Your problem with losing the character may be due to that last part of your regular expression: "(\w+)(?: |)ray" where I don't think the (?: |) is really necessary. There will always be a space before ray, so just make it "(\w+) ray" instead, right?

Sometimes using patterns like \w+ will yield unexpected results, but you can usually give more information to the regular expression parser to correct the problem. In some cases, using \w+? might fix it. In others, you just need a more narrow wildcard search.
Top

Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Reply #8 on Thu 12 May 2005 08:57 PM (UTC)
Message
Yeah, you're right. But it still wont work for me.

Patter:
^You are hit by a (green|yellow|violet|red|indigo|blue|orange|)(?:, | and | |)(green|yellow|violet|red|indigo|blue|orange|)(?:, | and | |)(green|yellow|violet|red|indigo|blue|orange|)(?:, | and | |)(green|yellow|violet|red|indigo|blue|orange|)(?:, | and | |)(green|yellow|violet|red|indigo|blue|orange|)(?:, | and | |)(green|yellow|violet|red|indigo|blue|orange|)(?:, | and | |)(green|yellow|violet|red|indigo|blue|orange) ray\.$

Simplicity is Divine | http://nogfx.org/
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #9 on Thu 12 May 2005 09:03 PM (UTC)

Amended on Thu 12 May 2005 09:06 PM (UTC) by Ked

Message
Larkin:
Quote:
Your problem with losing the character may be due to that last part of your regular expression: "(\w+)(?: |)ray" where I don't think the (?: |) is really necessary. There will always be a space before ray, so just make it "(\w+) ray" instead, right?


But then you have the problem of the last {?:, | and | |) group, which already includes the space, so you'll have two spaces in the end in certain cases.

CroX:
Quote:
Doesn't match..?


That's because I made a typo and you repeated it :P It should be:


^You are hit by a (indigo|yellow|green|violet|blue|red|orange|)(?:, | and | |)(indigo|yellow|green|violet|blue|red|orange|)(?:, | and | |)(indigo|yellow|green|violet|blue|red|orange|)(?:, | and | |)(indigo|yellow|green|violet|blue|red|orange|)(?:, | and | |)(indigo|yellow|green|violet|blue|red|orange|)(?:, | and | |)(indigo|yellow|green|violet|blue|red|orange|)(?:, | and | |)(indigo|yellow|green|violet|blue|red|orange|)(?: |)ray\.$


The very last captured group - (orange) originally - was missing a | to signify "orange or nothing". Hope it's right this time around, make sure all the colours are in there or it'll fail to match in some cases.

Quote:
Furthermore, \w+ is much slower? I switched recently to MC from zMUD because of its speed, and so far most of my triggers uses \w+..



"Slow" in MC's terms normally means a difference of some 0.0005 seconds. But it's only as fast as you make it. 
There are some tricks you can use to keep your entire system fast and it's a good idea to use them consistently, since 
0.0005 seconds might not be a big deal but little things build up to large problems. One of the major tricks with 
regexps is to make them as explicit as you can, since the less they have to guess, the faster they go, and the better 
you can predict what they'll do.


Edit: You have been slain by the formatting monster.
Top

Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Reply #10 on Thu 12 May 2005 10:27 PM (UTC)
Message
Actually.. I just found out what was wrong. I just wrote in some color that came to mind to test it. And, obviously, it wont fire to white.. Sorry. :P

Changed the pattern some, as there will always be atleast one color, so the | you added in the last (what's the term?) match-thingy shouldn't be there.

Anyways, I learned alot. Thank you all for the help!

Simplicity is Divine | http://nogfx.org/
Top

Posted by Flannel   USA  (1,230 posts)  Bio
Date Reply #11 on Thu 12 May 2005 11:21 PM (UTC)
Message
instead of explicitly listing the colors (and then having to edit them each time) you should think about using a variable.

And even then...
^You are hit by a ((indigo|yellow|green|violet|blue|red|orange)(?:,? | and |))+ ray\.$

Should work, and is oodles shorter (if we're insisting on lookaheads with null strings instead of optional quantifiers, otherwise it could be shorter still).

~Flannel

Messiah of Rose
Eternity's Trials.

Clones are people two.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


30,863 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.