TinTin++ Mud Client The TinTin++ message board

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
TinTin++ Mud Client

How to strip out unwanted parts of string?

 
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> Script Help
View previous topic :: View next topic  
Author Message
rw3iss



Joined: 17 Jul 2014
Posts: 23

PostPosted: Sun Nov 05, 2017 10:53 am    Post subject: How to strip out unwanted parts of string? Reply with quote

Heyas,

I'm trying to capture the mob name itself, in multiple cases when the end of the name line could have extra string (ie, wounded, health percentage, who it's attacking). These are the three cases:

Quote:
Some Mob Name.
Some Mob Name attacking you!.
Some Mob Name [wounded] [75%] attacking you!.


I use ascii codes to trigger the mob name, in other words all mobs start with '-M-_'.
So far I've been able to capture the line in all cases with:

Code:
#act {{^-M-_(.*)\.}} {
    #regexp {%2} {{^((.*) \[(.*)\] \[(.*)\] attacking(.*)|(.*) attacking(.*)|(.*)}  {#var mobName {&2};} {#show noeval};
    #show mobname: $mobName;
}


However, the $mobName only ever has the full mob line, ie. matching (.*) instead of the others, which includes all the optional text after its name.
Is there a way to write one regex or action that can capture just the mob name in all these cases, without any of the optional text after it?
Back to top
View user's profile Send private message
rw3iss



Joined: 17 Jul 2014
Posts: 23

PostPosted: Sun Nov 05, 2017 1:45 pm    Post subject: Reply with quote

I just did a multi-level regexp which kind of deals with match preference and works:


Code:
   #regexp {$lastMobName} {{(.*) \[(scratched|bruised|wounded|dying)\] (.*)attacking(.*)}} {#var mobName &2;} {
      #regexp {$lastMobName} {{(.*) attacking(.*)}} {#var mobName &2;} {
         #regexp {$lastMobName} {{(.*)}} {#var mobName &2;} {
            #show NOEVAL;
         }
      }
   }
   #show mobname: $mobName;
Back to top
View user's profile Send private message
balthus



Joined: 07 Jul 2009
Posts: 39

PostPosted: Sun Nov 05, 2017 4:48 pm    Post subject: Reply with quote

Try this:
Code:

^([^\[]*?)(\s*\[.+\]|)(\s*attacking you!|)\.


Matches all three of your examples.

The first set of brackets matches everything that doesn't contain a "[" from the start of the line onwards.

The second set matches anything from then on until the last "]" found (or nothing)

The third matches the "attacking you!" bit (or nothing).
Back to top
View user's profile Send private message
rw3iss



Joined: 17 Jul 2014
Posts: 23

PostPosted: Mon Nov 06, 2017 8:07 pm    Post subject: Reply with quote

Awesome, works great, thanks a lot balthus. One of my goals this year is to learn more regex. Thanks for the lesson :-)
Back to top
View user's profile Send private message
Scandum
Site Admin


Joined: 03 Dec 2004
Posts: 3770

PostPosted: Sun Nov 12, 2017 12:55 pm    Post subject: Reply with quote

Might be possible to simplify this slightly:

Code:

#act {^-M-_%1{ \[(.*?)\]|}{ \[(.*?)\%\]|}{ attacking you!|}.%*%*} {#showme 1 (%1) 2 (%2) 3 (%3) 4 (%4) 5 (%5) 6 (%6) 7 (%7)


The %*%* at the end is necessary because you shouldn't really be using parenthesis within braces. Not sure if that's easy to fix.

Remove the () and the %* at the end if you don't need the numbers, or don't mind using separate regex within the action to pull those values.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> Script Help All times are GMT - 5 Hours
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
Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads
TinTin++ Homepage

Powered by phpBB © 2001, 2002 phpBB Group