[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  problem with regexp and catching string

problem with regexp and catching string

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Mahony   (27 posts)  [Biography] bio
Date Tue 07 Jun 2016 07:45 AM (UTC)

Amended on Tue 07 Jun 2016 07:53 PM (UTC) by Nick Gammon

Message
Hi
I'm trying to catch name from this command and output:


swho 11 20 60
[ 26  Eldar  P+3] [     819 ] Mahony (^\Rhabdo/^)
[ 61  Diva   T+5] [     708 ] *AFK* Trachx <(=Watchmen=)>
[ 54  Eldar  M+5] [     222 ] [Advisor] Nienna [C]
[ 54  Eldar  M+5] [     221 ] *AFK* [Advisor] Xyz [C]
[ 22  Giant  W+7] [       4 ] (HARDCORE) Nightbird /|\Druid/|\
[ 25  Vamp   H+2] [       3 ] (Linkdead) Izabella  


I want to catch the number of gqs (so the %1) and the name. The name position varies depending on the *AFK*, Helper and so on before the name. So it could be %2 or %3 or even %4. I catch only %2 and %3. It is enough for me. But I have problems with it.

The trigger regexp is like this:


match="^\[.{15}\] \[\s+(\d+)\s+\] (.*?) (.*?)"
[ 26  Eldar  P+3] [     819 ] Mahony (^\Rhabdo/^)
%2 is Mahony
%3 is 
[ 51  Vamp   P+6] [     576 ] *AFK* Zetkax 
%2 is *AFK*
%3 is 
[ 54  Eldar  M+5] [     222 ] [Advisor] Nienna [C]
%2 is [Advisor]
%3 is 


Why is the %3 empty? I would expect the rest of the line behind the %2 to be in %3.

What somehow works for me is this match:


match="^\[.{15}\] \[\s+(\d+)\s+\] (.*) (.*)"
[ 26  Eldar  P+3] [     819 ] Mahony (^\Rhabdo/^)
%2 is Mahony
%3 is (^\Rhabdo/^)
[ 61  Diva   T+5] [     708 ] *AFK* Trachx <(=Watchmen=)>
%2 is *AFK* Trachx
%3 is <(=Watchmen=)>


But it is not 100% what I want.

What exactly I want to achieve is this:


[ 61  Diva   T+5] [     708 ] *AFK* Trachx <(=Watchmen=)>
%2 is *AFK*
%3 is  Trachx


What do I do wrong?
Thanks
-Mahony




[EDIT]
Formatting fixed, code tags added, forum codes escaped.
[Go to top] top

Posted by Fiendish   USA  (2,517 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Tue 07 Jun 2016 08:00 AM (UTC)
Message
In a regex pattern [...] means "anything in this set of symbols". [abc] will match either an a, b, or c. [abc]+ will match a, b, c, aa, ab, ac, ba, bb, bc, and so on. If you want to match the [ ] symbols themselves, you need to backslash escape them in the pattern.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Mahony   (27 posts)  [Biography] bio
Date Reply #2 on Tue 07 Jun 2016 11:16 AM (UTC)
Message
Oh crap. The copy/paste didn't work correctly and I didn't notice.
the match is
match="^\[.{15}\] \[\s+(\d+)\s+\] (.*) (.*)"

I see the problem in the end of the match in the (.*) (.*)"

sorry
[Go to top] top

Posted by Fiendish   USA  (2,517 posts)  [Biography] bio   Global Moderator
Date Reply #3 on Tue 07 Jun 2016 01:02 PM (UTC)
Message
Try this:


^.{20}\s*(\d+)\s\]\s([\*\[\(].+[\*\]\)] )*(\w+)


It should catch the number in %1 and the name in %3

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (23,000 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Tue 07 Jun 2016 08:06 PM (UTC)

Amended on Tue 07 Jun 2016 08:10 PM (UTC) by Nick Gammon

Message
Here's another way of doing it:


^\[.{15}\] \[\s+(\d+)\s+\](.*?)(?= \w) (\w+)


That starts the same as your original one. Then we have:


(.*?)    --> skip any characters, non greedily
(?= \w)  --> assert (without consuming characters) that we have a space followed by a letter
 (\w+)   --> match the space and capture the name


The lookahead assertion is the important part. The first .*? keeps matching until the assertion is true. That skips words that do not start alone (eg. skips *AFK* / [Advisor] / (Linkdead) ).




Output from your test data:


swho 11 20 60
[ 26  Eldar  P+3] [     819 ] Mahony (^Rhabdo/^)
%1 = 819
%2 = 
%3 = Mahony

[ 61  Diva   T+5] [     708 ] *AFK* Trachx <(=Watchmen=)>
%1 = 708
%2 =  *AFK*
%3 = Trachx

[ 54  Eldar  M+5] [     222 ] [Advisor] Nienna [C]
%1 = 222
%2 =  [Advisor]
%3 = Nienna

[ 54  Eldar  M+5] [     221 ] *AFK* [Advisor] Xyz [C]
%1 = 221
%2 =  *AFK* [Advisor]
%3 = Xyz

[ 22  Giant  W+7] [       4 ] (HARDCORE) Nightbird /|ruid/|
%1 = 4
%2 =  (HARDCORE)
%3 = Nightbird

[ 25  Vamp   H+2] [       3 ] (Linkdead) Izabella  
%1 = 3
%2 =  (Linkdead)
%3 = Izabella

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] 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.


13,297 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] 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.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]