Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ match a certain line of text - having probs with finding correct regex syntax
|
match a certain line of text - having probs with finding correct regex syntax
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Dakine
(22 posts) Bio
|
| Date
| Sun 19 Apr 2015 05:27 PM (UTC) Amended on Sun 19 Apr 2015 08:37 PM (UTC) by Dakine
|
| Message
| I'm trying to match as an example the following line of text via a trigger.
(13) (M)(G)(H) Ice Cold Arnold Palmer (120)
The first set of brackets can be in the format as
( 1) or (10) or absent. The number of potions.
second set of brackets "(M)(G)(H)" can be any combination e.g (M)(G) or (M) or (G) etc.
The name (Ice Cold..) I have put into a variable to match.
The last bracket will always be present and will be any number up to 201.
Currently I've got it to match too many things or nothing at all.
I have the following so far. I've tried using various online regex sites to decode it but end result is a headache.
to match: (13) (M)(G)(H) Ice Cold Arnold Palmer (120)
^\s*\((\s\d|\d*)\)\s(?:\((\w+)\)\s)*(@!variable)\s\((.*)\)$
Any help would be appreciated.
Thanks
Dak | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #1 on Sun 19 Apr 2015 07:51 PM (UTC) |
| Message
| It would help to post the whole thing, so we can check if things like "expand variables" are checked.
 |
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.
|
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #2 on Sun 19 Apr 2015 08:16 PM (UTC) Amended on Sun 19 Apr 2015 08:17 PM (UTC) by Nick Gammon
|
| Message
| This works as far as I can tell:
<triggers>
<trigger
custom_colour="2"
enabled="y"
expand_variables="y"
match="^\s*(\(\s?\d+\)){0,1}\s+((\([MGH]\)){0,})\s*@variable\s+\((\d+)\)$"
regexp="y"
send_to="2"
sequence="100"
>
<send>
%%1 = %1
%%2 = %2
%%3 = %3
%%4 = %4
</send>
</trigger>
</triggers>
Output:
%1 = (13)
%2 = (M)(G)(H)
%3 = (H)
%4 = 120
Explanation with each part labelled:
^\s*(\(\s?\d+\)){0,1}\s+((\([MGH]\)){0,})\s*@variable\s+\((\d+)\)$
1 2 3 4 5 6 7 8 9 a bc d ef g h i j k Lm n p q
1 = zero or more spaces
2 = start of group
3 = opening bracket in text
4 = optional space
5 = one or more digits
6 = closing bracket in text
7 = end of group
8 = group can occur 0 or 1 times
9 = one or more spaces
a = start of group of one or more of (G)(M)(H)
b = start of group of a single (G) or (M) or (H)
c = opening bracket in text
d = set consisting of M or G or H
e = closing bracket in text
f = end of group of a single (G) or (M) or (H)
g = previous group can occur 0 or more times
h = end of group of one or more of (G)(M)(H)
i = zero or more spaces
j = the variable "variable" expanded
k = one or more spaces
L = opening bracket in text
m = start of group
n = one or more digits
p = end of group
q = closing bracket in text
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Dakine
(22 posts) Bio
|
| Date
| Reply #3 on Sun 19 Apr 2015 08:37 PM (UTC) |
| Message
| Yes, sorry should have done that for clarity.
<trigger
enabled="n"
expand_variables="y"
ignore_case="y"
make_italic="y"
match="^\s*\((\s\d|\d*)\)\s(?:\((\w+)\)\s)*(@variable)\s\((.*)\)$"
name="LocatePots"
regexp="y"
script="PotsFound"
sequence="50"
>
</trigger>
function PotsFound(sName,sLine,wildcards)
debug("triggered potsFound")
end -- PotsFound
The trigger is sound in terms of it firing if the pattern was matched. I've tested it with a simple match without a variable to confirm.
Also that the @variable is actually being expanded. I've confirmed this with a simple trigger that sent the expanded variable to the output.
The regex pattern isn't quite right having tried it with a online pattern tester. I've tested it by replacing the variable with the actual text and no luck. It only succ
Also i realised that I posted the match with a missing escape character for one of the brackets. This was a typo and not in the code. I've edited the original post. | | Top |
|
| Posted by
| Dakine
(22 posts) Bio
|
| Date
| Reply #4 on Sun 19 Apr 2015 08:44 PM (UTC) Amended on Sun 19 Apr 2015 09:48 PM (UTC) by Dakine
|
| Message
|
Quote:
<triggers>
<trigger
custom_colour="2"
enabled="y"
expand_variables="y"
match="^\s*(\(\s?\d+\)){0,1}\s+((\([MGH]\)){0,})\s*@variable\s+\((\d+)\)$"
regexp="y"
send_to="2"
sequence="100"
>
<send>
%%1 = %1
%%2 = %2
%%3 = %3
%%4 = %4
</send>
</trigger>
</triggers>
Wow that's awesome. You find a solution in minutes that's literally taken me hours with no result.
Any secrets to a better understanding of regex? I'm not exactly a novice to it but often hard to see the wood for the trees..
That explanation you provided does give you an 'Oh now is see' moment..
What I should have done on reflection is broken it down more and tested for the different parts.
Thank you Nick | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #5 on Sun 19 Apr 2015 09:44 PM (UTC) |
| Message
|
 |
Regular expressions
- Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
- Also see how Lua string matching patterns work, as documented on the Lua string.find page.
|
What I do when I am struggling with a complicated regexp is to start small and build up.
So for example with yours, make it colour the matching line, echo the wildcards (like I did) and then start from the left.
So for example, get it to match:
Then:
Then add the item name, and so on. By leaving off the trailing $ it will match providing the start is right.
If you try to match the whole thing at once a space out of place can cause the whole thing to fail. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Dakine
(22 posts) Bio
|
| Date
| Reply #6 on Sun 19 Apr 2015 09:53 PM (UTC) |
| Message
| Yes that makes a lot of sense. Thanks for the solution and pointers.
Dak | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
27,641 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top