 |
chamenas Wizard

Joined: 26 Mar 2008 Posts: 1547
|
Posted: 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. |
|
|
|
 |
Larkin Wizard

Joined: 25 Mar 2003 Posts: 1113 Location: USA
|
Posted: Sat Mar 29, 2008 2:21 pm |
Use #REGEX instead of #TRIGGER.
|
|
|
|
 |
chamenas Wizard

Joined: 26 Mar 2008 Posts: 1547
|
Posted: Sat Mar 29, 2008 3:45 pm |
So it just registers as a regex trigger that way?
|
|
|
|
 |
chamenas Wizard

Joined: 26 Mar 2008 Posts: 1547
|
Posted: Sat Mar 29, 2008 3:49 pm |
Also, isn't \w supposed to be %w?
|
|
|
|
 |
Larkin Wizard

Joined: 25 Mar 2003 Posts: 1113 Location: USA
|
Posted: 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. |
|
|
|
 |
chamenas Wizard

Joined: 26 Mar 2008 Posts: 1547
|
Posted: Sun Mar 30, 2008 1:09 am |
Still confused as to why it's only taking the last letter.
|
|
|
|
 |
Dharkael Enchanter

Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Sun Mar 30, 2008 1:42 am |
Because thats what you told it to do
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." |
|
|
 |
chamenas Wizard

Joined: 26 Mar 2008 Posts: 1547
|
Posted: Sun Mar 30, 2008 1:50 am |
Thanks
|
|
|
|
 |
Dharkael Enchanter

Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: 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." |
|
|
 |
Fang Xianfu GURU

Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: 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.
|
|
|
|
 |
Dharkael Enchanter

Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: 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." |
|
|
 |
|
|
|