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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Sat Mar 29, 2008 1:23 pm   

Regex - Setting in Command form
 
How do I make this:

#TRIGGER sleep {^You go to sleep(?: .*(\w+)|)\.$} {posn=sleep;bed=%1} {General Triggers}

Enter in as a regex pattern (you know when you can select it at the bottom of the gui.

Also, it is only taking the last letter, not last word. But I'm not versed enough in regex yet to know why.
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Sat Mar 29, 2008 2:21 pm   
 
Use #REGEX instead of #TRIGGER.
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Sat Mar 29, 2008 3:45 pm   
 
So it just registers as a regex trigger that way?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Sat Mar 29, 2008 3:49 pm   
 
Also, isn't \w supposed to be %w?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Sat Mar 29, 2008 3:49 pm   
 
Yes. You can use #TRIGGER {pattern} {command} {class} {regex} to setup a regex trigger, but it's easier to just use #REGEX instead.

No. In regex, \w represents a "word-type character" (alphanumeric, basically). The %w is for the zMUD patterns, which are not regex.
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Sun Mar 30, 2008 1:09 am   
 
Still confused as to why it's only taking the last letter.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Sun Mar 30, 2008 1:42 am   
 
Because thats what you told it to do Wink
The .* in your pattern means match as many characters (except newline) as you can without breaking the rest of the pattern (Its greedy).
\w+ means match at least one word character and as many word characters as you can without breaking the rest of the pattern(also greedy).
So .* matches every character it can after sleep until the last character before the period.
It leaves that character because that will still allow \w+ to match.

To capture the last word on a line that begins with You go to sleep and ends with a period
try this pattern
Code:
^You go to sleep.*(\b\w+)\.$


The greedy .* is still there but the \b forces the \w+ to match starting at the beginning of a word forcing .* to leave a whole word for \w+
I removed the enclosing (?:) and the the empty alternative because it doesn't seem to serve a purpose.
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Sun Mar 30, 2008 1:50 am   
 
Thanks
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Sun Mar 30, 2008 2:05 am   
 
No problem.
If there is a possibility that the final word might include an apostrophe.
change the pattern to
Code:
^You go to sleep .*(?<=\s)([\w']+)\.$
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Sun Mar 30, 2008 10:32 am   
 
That seems a bit complex when all you need to do is change .* to .*? to make the * non-greedy.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Sun Mar 30, 2008 3:59 pm   
 
You're correct that is simpler, much nicer to look at, but less efficient as regexes go.
In this case it's probably a much better choice since Chamenas was already confused.
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion 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