Gammon Forum
Entire forum
MUSHclient
General
Need help to get started with simple regexp
Need help to get started with simple regexp
|
It is now over 60 days since the last post. This thread is closed.
  Refresh page
Posted by
| Tetnao
(8 posts) bio
|
Date
| Thu 07 Oct 2010 05:33 PM (UTC) |
Message
| I'm just starting to pick up a little coding skills so any help is greatly appreciated. I am trying to make a very simple regex alias to do the following:
Spell: call lightning
Typing this out is quite tedious, because I'll have to do:
Case 1) c 'call lightning'
because 'call' is keyword already reserved for another spell. In the case where I want to target a mob which is not my main target, I'll have to do:
Case 2) c 'call lightning' 2.troll
I tried many variations, but this is the closest that I got to making it work:
Alias: ^cl(.*)
Send: c 'call lightning' %1
This works generally, but it'll also work for when I type:
'cl 2.troll blah blah blah' (meaning it doesn't just check for 1 word after 'cl')
and
'cl2.troll' (without space)
I think I know where my mistake is, * is for repeating 0 or more times. Well I have 2 questions
1) How do I specify only 1 word after 'cl', seperated from 'cl' by a space, and then grab that input as %1?
2) How do I indicate something as %1 %2 or %3 etc? Do I do it with the () brackets? (I actually just kinda copy that regex from somewhere, and change it around abit but don't really understand it..)
Thanks in advance! | top |
|
Posted by
| Worstje
Netherlands (899 posts) bio
|
Date
| Reply #1 on Thu 07 Oct 2010 05:52 PM (UTC) Amended on Thu 07 Oct 2010 05:53 PM (UTC) by Worstje
|
Message
| 1) Try ^cl (\w+)$ which means a line that starts with cl<space>, followed by at least one letter of the 'wordy' type, which basically means letters and numbers, 'followed' by the end of the line. If you want the space to be part of the capture, move it into the (), but remember that would also mean it becomes a part of the capture.
2) Correct. The (...) signify a capture group. However, when a capture looks like (?:...), e.g. there's a questionmark and colon at the start, then it is a non-capturing group which mainly serves a purpose in more complicated regular expressions. | top |
|
Posted by
| Tetnao
(8 posts) bio
|
Date
| Reply #2 on Thu 07 Oct 2010 07:02 PM (UTC) |
Message
| Thanks! The example was great help!
^cl( \w)?$
was what I needed :P | top |
|
Posted by
| Worstje
Netherlands (899 posts) bio
|
Date
| Reply #3 on Thu 07 Oct 2010 07:16 PM (UTC) |
Message
| Great if it works for you, although I don't see the connection to the problem you posted.
Right now, you use a single \w, which means a single character. The ? on the outside of the group means that \w gets repeated never or just once.
E.g. it would match on cl t and cl, but NOT on cl troll. Are you sure that is what you need? | top |
|
Posted by
| Tetnao
(8 posts) bio
|
Date
| Reply #4 on Fri 08 Oct 2010 03:44 AM (UTC) |
Message
| Oops, I meant
^cl( \w*)?$
i used an * instead of the + so that when i accidentally typed 'cl ' with that extra space, and did not type anything behind, it probably is a typo and i still cast call lightning on my main target :P but that's just some modifications to suit my needs, thanks for the basic idea!
I have another question though, I just realised it doesnt match a . (period), in such a case when i type:
cl 2.troll
or
cl 39.troll
it does not work because \w does not contain the . (period) character. This is what I did:
^cl( [{0-9}+\.]?\w*)?$
and
^cl( [\d+\.]?\w*)?$
which I think is the same thing.. I think I have the logic right this time, but it doesn't seem to work, am I doing something wrong?
Thanks! | top |
|
Posted by
| Twisol
USA (2,257 posts) bio
|
Date
| Reply #5 on Fri 08 Oct 2010 03:54 AM (UTC) Amended on Fri 08 Oct 2010 03:59 AM (UTC) by Twisol
|
Message
|
Tetnao said:
Oops, I meant
^cl( \w*)?$
i used an * instead of the + so that when i accidentally typed 'cl ' with that extra space, and did not type anything behind, it probably is a typo and i still cast call lightning on my main target :P but that's just some modifications to suit my needs, thanks for the basic idea!
I would prefer this one:
It allows for optional whitespace at the beginning or end of the alias; any amount of whitespace between the alias and its arguments; and only captures the argument, not the space before it as well.
Tetnao said: cl 2.troll
Try this:
^\s*cl(?:\s+((?:\d+\.)?\w+))?\s*$
It allows for an optional numeric prefix on the target. In a less arcane format for clarity: cl [[number.]target]
[EDIT]: Here, I'll break it down for you.
^
\s* Optional whitespace before the alias
cl
(?: Non-capturing group
\s+ Whitespace between the alias and the argument
( Capturing group; this is the actual argument
(?:\d+\.)? Optional number-and-dot before the name
\w+ Name of the target
)
)? Make the whole argument optional
\s* Optional whitespace after the alias
$
|
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | top |
|
Posted by
| Worstje
Netherlands (899 posts) bio
|
Date
| Reply #6 on Fri 08 Oct 2010 04:39 AM (UTC) |
Message
| Or you know, he could simply have adjusted his original to ^cl( [\w.]*)?$, which simply adds the . as an accepted character rather than add a lot of extra complicated references. Of course, multiple . will be allowed and such, but I don't think that could hurt much in this case. | top |
|
Posted by
| Nick Gammon
Australia (22,975 posts) bio
Forum Administrator |
Date
| Reply #7 on Fri 08 Oct 2010 04:44 AM (UTC) |
Message
|
Tetnao said:
Case 1) c 'call lightning'
because 'call' is keyword already reserved for another spell. In the case where I want to target a mob which is not my main target, I'll have to do:
Case 2) c 'call lightning' 2.troll
I tried many variations, but this is the closest that I got to making it work:
Alias: ^cl(.*)
A lot of this is covered here:
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Twisol
USA (2,257 posts) bio
|
Date
| Reply #8 on Fri 08 Oct 2010 04:53 AM (UTC) Amended on Fri 08 Oct 2010 09:33 AM (UTC) by Twisol
|
Message
|
Worstje said:
Or you know, he could simply have adjusted his original to ^cl( [\w.]*)?$, which simply adds the . as an accepted character rather than add a lot of extra complicated references. Of course, multiple . will be allowed and such, but I don't think that could hurt much in this case.
It also includes the space as part of the argument capture, which can really throw things off if you don't expect it. Yeah, mine is more complicated, but it's also more robust.
Train of thought:
^cl$ -- base alias
^cl (\w+)$ -- add the argument
^cl (\d+\.\w+)$ -- add the number prefix
^cl ((?:\d+\.)?\w+)$ -- make the prefix optional
^cl(?: ((?:\d+\.)?\w+))?$ -- make the argument optional
^\s*cl(?: ((?:\d+\.)?\w+))?\s*$ -- allow extra spaces at the start and end
^\s*cl(?:\s+((?:\d+\.)?\w+))?\s*$ -- allow any number of spaces before the argument
[EDIT]: I accidentally used \s* instead of \s+ in the last line there. Fixed. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | top |
|
Posted by
| Nick Gammon
Australia (22,975 posts) bio
Forum Administrator |
Date
| Reply #9 on Fri 08 Oct 2010 05:17 AM (UTC) |
Message
|
Tetnao said:
2) How do I indicate something as %1 %2 or %3 etc? Do I do it with the () brackets? (I actually just kinda copy that regex from somewhere, and change it around abit but don't really understand it..)
Rather than adding all the extra stuff to suppress capture groups, it could be easier to just name your groups.
For example:
^cl( +(?<name>(\d+\.)?\w+))?$
Now in this case the part of the regexp that is the mob's name is called "name", so you can refer to it as:
Then you don't need to care if it is really wildcard 1, 2 or 3 etc.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Tetnao
(8 posts) bio
|
Date
| Reply #10 on Fri 08 Oct 2010 05:32 AM (UTC) |
Message
| Wow, those were very detailed explanation and some neat tricks, and is working like a charm for me :)
I see different opinions here, but I was able to learn from each idea/suggestion that you guys gave :)
Thanks worstje, twisol and nick! | 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.
26,792 views.
It is now over 60 days since the last post. This thread is closed.
  Refresh page
top
Quick links:
MUSHclient.
MUSHclient help.
Forum shortcuts.
Posting templates.
Lua modules.
Lua documentation.
Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.