Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD Beta Forum
SteveThing
Beginner


Joined: 29 Aug 2010
Posts: 12

PostPosted: Tue Sep 21, 2010 10:20 pm   

Regex and Triggers
 
Having some issues with a pattern. I've referenced many sites on regex and I am having no luck. Here are the strings I am triggering:

Code:

  | Pierce: barely protected   (75    )|  Hungry: Yes              |
  |  Slash: barely protected   (75    )| Thirsty: Yes              |
  |   Bash: barely protected   (75    )|   Drunk: No               |
  |  Magic: defenseless        (100   )|Position: Sleeping         |


I want to create 4 triggers to record my condition (hungry, thirsty, drunk) and my position. I have tried the following patterns:

Code:

^  ~| Pierce: * %s~(%d%s~)|  Hungry: (Yes|No)%s|$
^\s\s\|\sPierce:\*\(\d+\s+\)\|\s\sHungry:\s(Yes|No)\s+\|$
^\s\s\|\s\sSlash:\*\(\d+\s+\)\|\sThirsty:\s(Yes|No)\s+\|$
^\s\s\|\s\s\sBash:\s\w+\(\d+\s\)\|\s\sDrunk:\s(Yes|No)\s+\|$
^\s\s\|\s\sMagic:\s\w+\(\d+\s\)\|Position:\s(Yes|No)\s+\|$


I cannot get these patterns to match. They look right, but obviously I am missing something. Any help would be appreciated. Oh yeah, I am using Beta 3.28.
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Tue Sep 21, 2010 10:26 pm   
 
Instead of creating a single trigger to match multiple lines, it is better to create a trigger with multiple trigger states. Somebody else can hopefully help with the exact syntax for that.

Using the $ to match a newline only works in a normal CMUD trigger, not a regex trigger. I believe you can use \n in a regex trigger for multiple lines, but I've never tested it and not sure it will work. In any case, using $ to match multiple lines is handled very inefficiently in CMUD. Multistate triggers are much better and more reliable.

See the Multistate Trigger documentation page for more info until somebody else can give you more details.
Reply with quote
Fizban1216
Apprentice


Joined: 03 Feb 2007
Posts: 170

PostPosted: Tue Sep 21, 2010 10:38 pm   
 
You can actually highlight it on the screen, right click, and select make trigger. By far the easiest way to set up a multi-line trigger.

Code:

  ~| Pierce:*Hungry: (%w) %s~|
  ~|  Slash:*Thirsty: (%w) %s~|
  ~|   Bash:*Drunk: (%w %s~|
  ~|  Magic:*Position: (%w) %s~|
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Tue Sep 21, 2010 10:48 pm   Re: Regex and Triggers
 
SteveThing wrote:
Having some issues with a pattern. I've referenced many sites on regex and I am having no luck. Here are the strings I am triggering:

Code:

  | Pierce: barely protected   (75    )|  Hungry: Yes              |
  |  Slash: barely protected   (75    )| Thirsty: Yes              |
  |   Bash: barely protected   (75    )|   Drunk: No               |
  |  Magic: defenseless        (100   )|Position: Sleeping         |




Code:

\|\s+(?:Pierce|Slash|Bash|Magic)\:\s[\w ]+\s+\(\d+\s+\)\|(?:\s+)?(Hungry|Thirsty|Drunk|Position)\:\s([\w ]+)


With this, %1 should always be either: Hungry, Thirsty, Drunk, or Position, so you can make one trigger, with a #SWITCH to do different things depending on which of the four it is, and then use %2 to get the proper value. If all of the potential %2s are one word, you can change it to: \w+, you can exert even tighter control if you know all the positions, by changing it to: (Yes|No|Sleeping|etc...)

Edit: To explain what is being done here:
\s+ - allows you to match one or more spaces, the number of spaces can be variable. This is pretty handy to deal with any sort of formatting that changes between lines, as is the case here.
() - By surrounding various words in parentheses and then using | dividers, we can make an OR statement. We're telling to match one of the words within the parentheses. If none of the words inbetween the parentheses are there, then it will not match.
?: - Putting this just inside the parentheses will tell CMUD that we don't want to remember what's inside as part of the %x (%1, %2, ...) capture syntax, since we capture in CMUD by surrounding something in parentheses.
? - Using this after encapsulating something in parentheses qualifies whatever is in the parentheses as optional. Essentially we're saying "match it if it's there, but if it's not there, don't worry about it". In this case, we use it around the \s+ towards the end, because, for some reason, Position doesn't have a space between it and its preceding |, so we need to make it optional.
[\w ]+ - Using square brackets around something tells CMUD that we're making an AND match, meaning we'll grab anything between the brackets that shows up. This works sort of like the OR of parentheses until you add the + operator outside. The + operator outside says to keep matching until it doesn't match anymore, and then it moves onto the next match in the regex. So if we put \w and a space in between the brackets (I assume we could also have done [\w\s]+) then we're telling it it can match any word without numbers or special characters. This is a tighter match than .* (match anything) and so more useful if you know that only words can be used in whatever is being matched. If you needed to match commas, apostrophes or hyphens, you can simply add them to it ([\w ,-']+) etc...
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
SteveThing
Beginner


Joined: 29 Aug 2010
Posts: 12

PostPosted: Wed Sep 22, 2010 2:01 am   
 
chamenas,

Wow! That makes ALOT more sense. That should be in the help file! I will definately give this a shot.

THANK YOU!
Reply with quote
SteveThing
Beginner


Joined: 29 Aug 2010
Posts: 12

PostPosted: Wed Sep 22, 2010 2:06 am   
 
Grr, pattern doesn't match. This is getting annoying.
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Wed Sep 22, 2010 3:09 am   
 
Pattern matches just fine for me, I tested it. Do you have "Regular Expression" checked off in the bottom right-hand corner?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Sep 23, 2010 6:55 pm   
 
SteveThing, did this work? It works for me, I'd like to know if you got it working too. Send me a PM for further communication if there's problems. :)
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD Beta Forum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net