|
zoom-ping Newbie
Joined: 09 Feb 2009 Posts: 3
|
Posted: 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? |
|
|
|
Vijilante SubAdmin
Joined: 18 Nov 2001 Posts: 5182
|
Posted: 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 |
|
|
|
zoom-ping Newbie
Joined: 09 Feb 2009 Posts: 3
|
Posted: 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).
|
|
|
|
calesta Apprentice
Joined: 07 Dec 2008 Posts: 102 Location: New Hampshire, USA
|
Posted: 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)?(.*)$ |
|
|
|
|
zoom-ping Newbie
Joined: 09 Feb 2009 Posts: 3
|
Posted: 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? |
|
|
|
|
|