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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
zoom-ping
Newbie


Joined: 09 Feb 2009
Posts: 3

PostPosted: Mon Feb 09, 2009 11:23 am   

Regex help wanted
 
Hello.

I'm having problems getting a value using a regex pattern.
The pattern I wrote looks like this:
Code:
^\s+-\s([(]G|R|H|I|W|T|Angry[)]\s?)*?(.*?)$

And I'm trying to match something like this:
Code:
     - (T)(G)(W) A benevolent priestess

It seems to make the match, but using %2 I get the whole thing including the (X) flags.
Can someone please tell me what am I doing wrong?
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Mon Feb 09, 2009 12:21 pm   
 
Code:
^\s+- (Angry\[|\]|[\(GRHIWT\)])*? ?(.*?)$

_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
zoom-ping
Newbie


Joined: 09 Feb 2009
Posts: 3

PostPosted: Mon Feb 09, 2009 3:18 pm   
 
Works as good as my trigger. Still get the mob name with flags included. I'm not good with regexp and I don't understand the necessity to add \ to | and T. Can you comment your solution a bit? Just to be clear, here are the flags I want to ignore: (I)(H)(G)(R)(D)(X)(T)(W)(Angry).
Reply with quote
calesta
Apprentice


Joined: 07 Dec 2008
Posts: 102
Location: New Hampshire, USA

PostPosted: Mon Feb 09, 2009 5:13 pm   
 
The \ applies to the character after it, not before it. In a regex, the [] indicate a character class and the () indicate a grouping, so they have to be escaped in order to match them literally.

I believe the reason it doesn't work is that the part intended to match the flags is non-greedy (due to the ? after the first *). This combined with the fact that the regex ends with .* guarantees that the flags portion at the beginning will never consume anything. If you remove the ? after that first *, Vijilante's regex should work fine as long as (Angry) isn't in the flags, looks like he expected a different format for that or something.

I think you may have intended that ? after the flags part to mean that there can be no flags instead of a non-greedy match though, so I combined the two to come up with this one that seems to work for all the cases I tried. I'm no expert on whether its the most efficient regex or not though. Note the match is in %1 instead of %2 since I assumed you're just discarding the flags:
Code:
^\s+-\s(?:(?:(?:Angry|[()IHGRDXTW])\s?)*\s)?(.*)$
Reply with quote
zoom-ping
Newbie


Joined: 09 Feb 2009
Posts: 3

PostPosted: Tue Feb 10, 2009 6:45 am   
 
Thanks for the help. My friend pointed out the same error. I removed the '?'and tweaked it for larger number of flags that I potentially missed and it works fine now. Here's how it looks like now:
Code:
^\s+-\s([(][A-Za-z]+[)])* ?(.*?)$


Now I have another question: Is it possible to expand vars in regex like in zmud trigger patterns?
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD 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