|
Bremen Novice
Joined: 26 Dec 2002 Posts: 33 Location: USA
|
Posted: Tue Mar 11, 2008 2:51 am
ZMUD Regex # of matches or removing words |
I want to remove multiple words matching the same pattern in a line, but I'm not sure if there's a way to know the # of times a #regex matched in a pattern. I know they get saved to %1..%99, but if I don't know how many there are...
Alternatively, if someone can think of an elegant way to do it, I'd be fine with that. Here's the scenario:
A room has an Exits line listing the available exits:
[Exits: north east south west up down other]
Each entry (north, east, etc) may either appear or not appear in the list, or may be enclosed in parentheses, e.g.:
[Exits: (north) south (west)]
The order will always be the same (NESWUDO), but that shouldn't matter since they're all matchable with %w or \a+.
The tricky part is that I want to remove any entries surrounded by parentheses, such that
[Exits: (north) south (west)]
becomes
[Exits: south]
Additionally, if there are no entries left ([Exits: ]), I'd like it to insert "none" ([Exits: none]).
Right now I'm accomplishing it by capturing the line, #gag'ing it, and #say'ing it with a %subregex(), but I'd much prefer being able to do it in-line or with a PSUB than having to rely on #gag/#show. Or any alternative solutions would also be welcomed.. |
|
_________________ --Bremen, zMUD 7.21 on Windows 7 x64 |
|
|
|
Bremen Novice
Joined: 26 Dec 2002 Posts: 33 Location: USA
|
Posted: Tue Mar 11, 2008 3:22 am |
So I went from scratch and started with this (ignoring Other exits because I really don't care about them for now):
(1) ^[Exits\:(?: north)?( \(north\))?(?: east)?( \(east\))?(?: south)?( \(south\))?(?: west)?( \(west\))?(?: up)?( \(up\))?(?: down)?( \(down\))?]
From that I tried to generalize down to:
(2) ^[Exits\:(?:(?: \a+)?( \(\a+\))?)+]
except that it either matches too much (greedy) or too little (lazy).
For the sample line of:
[Exits: (north) east south (west)]
(1) will match %1=(north), %4=(west)
(2) will match %1=(west)
For (1) I have to manually do something silly like:
#IF (!%null(%1)) {#PSUB "" %x1}
#IF (!%null(%2)) {#PSUB "" %x2}
...
and (1) is also lame/inelegant. Ideally I'd like to manipulate (2) to match each instance AND have a way (with either (1) or (2)) to loop over the matches.. |
|
_________________ --Bremen, zMUD 7.21 on Windows 7 x64 |
|
|
|
Bremen Novice
Joined: 26 Dec 2002 Posts: 33 Location: USA
|
Posted: Tue Mar 11, 2008 3:41 am |
Alternative solution using #sub (it just struck me that #regex should be the way to do it, other than lousy zmud limitations...):
^(~[Exits~: *~])$ -> #SUB {%subregex(%1," \(\a+\)","")} |
|
_________________ --Bremen, zMUD 7.21 on Windows 7 x64 |
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Wed Mar 12, 2008 6:50 am |
This should do what you want, but you'll lose color information. For some reason PSUB and the Reparse option didn't play nice together.
#TRIGGER {(^~[Exits:*)( ~(%w~))(*~])} {#echo %1;#IF (!%null( %1)) {#SUB %1%3}} "" {reparse} |
|
_________________ Asati di tempari! |
|
|
|
Bremen Novice
Joined: 26 Dec 2002 Posts: 33 Location: USA
|
Posted: Wed Mar 12, 2008 8:41 am |
I don't lose color information because the whole line is the same color, so #sub preserves that. But..uh..wow, that's hax. Serious hax. I wasn't aware you could use the other trigger states for single-state triggers, that's not even hinted at in the Advanced Triggers page that I've read a billion times. Well played, at some point I was trying to figure out how to have the right number of reparse #cond's, but obviously this is the way to do it. Total hax.
|
|
_________________ --Bremen, zMUD 7.21 on Windows 7 x64 |
|
|
|
|
|