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.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Regexpes - Need help with designing one.

Regexpes - Need help with designing one.

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


Posted by Rynne   France  (5 posts)  Bio
Date Fri 18 Jan 2008 01:45 PM (UTC)

Amended on Fri 18 Jan 2008 01:47 PM (UTC) by Rynne

Message
Hello folks, I have a little problem.

I need to capture either

"You shove Bob."

or

"Bob shoves you."

but not

"Bob shoves John."


All while making sure that "Bob" always gets assigned to the same wildcard. Using regexpes. In only one trigger.

So far I came up with "((.*?) shoves you|You shove (.*?))\."

But somehow it feeds the whole line ("You shove Bob") in the first wildcard, and "Bob" in either the 2nd or the 3rd wildcard. (the other one being an empty string)

I need your help.
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #1 on Fri 18 Jan 2008 03:40 PM (UTC)

Amended on Fri 18 Jan 2008 03:42 PM (UTC) by Worstje

Message
That is because parentheses denote captured groups, as in '(' + group + ')'. So since the outer group is the first one it encounters, it will catch it.

There's a simple way to negate this behaviour, and that is to specify it not to capture, like this:

(?:(.*?) shoves you|You shove (.*?))\.

That could be simplified if you knew each name was only one word:

(?:(\w+) shoves you|You shove (\w+))\.

And if you preferred, you could get rid of the entire non-capturing group all together like this:

(\w+) shoves? (\w+)\.

The ? means 0 or 1 character of the group before it - in this case, the letter s. By checking the second captured group and seeing if it equals 'you', you are able to tell what case of the trigger it is (if you needed it). Additionally, I added a ^ (=beginning of line) in front and a $ (=end of line) at the end to signify that this is an entire line of output and not part of something like "Poo smells. Poo shoves the kingdom. Pee conquers all!" which your current trigger would fire on.

Edit: Oops, sleepiness. Misread it a bit, but you could of course perform the 'you' check on the first wildcard too, if you needed. But probably, the best solution would remain to be:

^(?:(\w+) shoves you|You shove (\w+))\.$
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Fri 18 Jan 2008 07:07 PM (UTC)

Amended on Fri 18 Jan 2008 07:08 PM (UTC) by Nick Gammon

Message
Quote:

All while making sure that "Bob" always gets assigned to the same wildcard. Using regexpes. In only one trigger.


How about this?


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="(?J)((?P&lt;who&gt;.*?) shoves you|You shove (?P&lt;who&gt;.*?))\."
   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>who is %&lt;who&gt;</send>
  </trigger>
</triggers>


I have used a named wildcard here (who) to capture the name. I also used the "allow duplicate names" option (?J).

Now, whichever branch matches, it still makes "who" be Bob.

Here is my test:


You shove Bob.
who is Bob
Bob shoves you.
who is Bob
Bob shoves John.
(no match)



You need to see http://www.mushclient.com/pasting to get a proper copy of this, inside the match text &lt; is really a < symbol.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Rynne   France  (5 posts)  Bio
Date Reply #3 on Sat 19 Jan 2008 08:35 PM (UTC)
Message
Thanks muchly, I got it to work.
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.


12,866 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.