 |
stormbarrier Beginner
Joined: 09 Jul 2012 Posts: 11
|
Posted: Thu Aug 02, 2012 4:38 pm
RegEx named capturing group in Trigger Patterns |
Does anyone know if there is a way to refer to a named capturing group from a trigger pattern?
For example:
Code: |
^\[Exits: (?<dirs>.+) \] |
cMUD seems to recognize the syntax for the named capturing group, but I don't know of a way to utilize its value by name as either a backreference or a captured pattern.
Understanding this is a simplified example, I can certainly use the captured pattern with a simple "%1", but I'm curious what the syntax would be to refer to the "dirs" name that I gave the capturing group. I can't seem to find this in the help files anywhere, but considering the trigger still functions and matches correctly, I assume it is a supported feature. |
|
|
 |
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Thu Aug 02, 2012 4:48 pm |
Try %param(dirs).
|
|
|
 |
stormbarrier Beginner
Joined: 09 Jul 2012 Posts: 11
|
Posted: Thu Aug 02, 2012 4:54 pm |
Good thought - but that didn't work. That is just returning the entire line, which is the same as using %param(). Any other thoughts?
|
|
|
 |
charneus Wizard

Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Thu Aug 02, 2012 5:31 pm |
As far as I know, the regex itself is supported, but named captures are not within zscript. If you find a way, let me know. I've got a friend who has been dying to do that.
|
|
|
 |
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Thu Aug 02, 2012 5:50 pm |
That's strange, it works fine for me... This is the trigger I'm testing with:
Code: |
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
<trigger priority="99900" regex="true" copy="yes">
<pattern><![CDATA[^\[Exits: (?<dirs>.+) \]]]></pattern>
<value>#print %param(dirs)</value>
</trigger>
</cmud> |
Then when I "#show [Exits: foo ]", it prints "foo". |
|
|
 |
charneus Wizard

Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Thu Aug 02, 2012 7:32 pm |
Oh, very interesting. I'll have to add that to my internal knowledge. :D
|
|
|
 |
stormbarrier Beginner
Joined: 09 Jul 2012 Posts: 11
|
Posted: Thu Aug 02, 2012 9:05 pm |
OK. I played around with this a bit and realized what it is doing. %param(<type anything or nothing here>) is simply concatenating all of the capture groups and returning that string. For this purpose, the reason %param(dirs) was working for you, but not for my testing, is because you only had one capture group.
I was testing with multiple capture groups, which essentially captured the whole line, but in separate chunks. When I called %param(dirs), it was returning each of the parameters, but it appeared to me that it was the whole line. If you were to take your test and call %param(nonsensetest) or %param(), you'd still see 'foo', because it is the only thing you captured.
So, while %param does return values, it is still not exactly what I'm looking for. I suppose cMUD could have been coded to play nice with named capture groups, but not provide a way to reference them. That just seems a bit silly to me if that is the case. |
|
Last edited by stormbarrier on Thu Aug 02, 2012 10:21 pm; edited 2 times in total |
|
|
 |
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Thu Aug 02, 2012 9:43 pm |
Hmm, you're right. That feels buggy to me.
|
|
|
 |
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Fri Aug 03, 2012 12:13 am |
It's not exactly buggy. It was simply trying to interpret "dir" as an integer, which to Cmud evaluated as %null, so it was the equivalent of %param(). The problem is, as Charneus said, Cmud does not implement named capturing groups. You need to use %1 or %param(1) in Cmud.
|
|
|
 |
MattLofton GURU
Joined: 23 Dec 2000 Posts: 4834 Location: USA
|
Posted: Fri Aug 03, 2012 1:48 am |
Not in regex, at least. Zscript patternmatching (moreorless regex made easy) has a way to apply a name to your capture group:
#trigger {...($varname:pattern goes here)...} |
|
_________________ EDIT: I didn't like my old signature |
|
|
 |
Taz GURU
Joined: 28 Sep 2000 Posts: 1395 Location: United Kingdom
|
Posted: Fri Aug 03, 2012 7:55 pm |
So based on what Matt said how about
Code: |
^\[Exits: (?dirs:your pattern) \] |
and then in script use
Code: |
#echo {match 1: @dirs} |
or have I got the wrong end of the stick?
|
|
_________________ Taz :) |
|
|
 |
stormbarrier Beginner
Joined: 09 Jul 2012 Posts: 11
|
Posted: Fri Aug 03, 2012 10:06 pm |
Taz wrote: |
or have I got the wrong end of the stick? |
You've got the Zscript end of the stick. My question was in reference to the RegEx end. But I appreciate everyone taking a look at this. |
|
|
 |
Taz GURU
Joined: 28 Sep 2000 Posts: 1395 Location: United Kingdom
|
Posted: Sun Aug 05, 2012 11:25 am |
Except that is RegEx and captures whatever pattern is in the parenthesis to the variable @dirs it is just a slight modification of yours, ?<dirs> becomes ?dirs: instead.
|
|
_________________ Taz :) |
|
|
 |
stormbarrier Beginner
Joined: 09 Jul 2012 Posts: 11
|
Posted: Mon Aug 06, 2012 3:02 pm |
Thanks Taz. I don't have any trouble doing this in zscript, but the point of the thread was to see if RegEx supports the named capture groups, not translating it to zscript. I was hoping it would so that I could make use of name backreferences in the regex pattern and also be able to more clearly utilize the capture groups in complex patterns. My "Exits" line was just an example for familiarity and simplicity's sake.
EDIT: I should apologize, Taz. I went back and reread your post and it made perfect sense this time. Not sure why I didn't understand clearly the first time, but your solution does, in fact, work perfectly fine with RegEx for named capturing groups. Thank you!! |
|
|
 |
|
|