Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
chaossdragon
Novice


Joined: 09 Apr 2008
Posts: 42

PostPosted: Wed Sep 21, 2016 3:08 pm   

REGEX pattern quandry (can be a zScript trigger if that helps)
 
I'm still learning the in's/out's of REGEX and I honestly don't know if this is possible...

I play on 3kingdoms and have attempted to utilize the G-Script MIP from the library to emulate the Portal GT hpbar/guages in cmud.
for the most part this has been a success... until I get to the gLine1/gLine2 parts.

Sample : <gND:> <yYes><g[><y98%><g]> <g[><yTRS><g|><yAA><g]> <gSI: [><y41.1761%><g]>

The problem that I face is the section "<yTRS><g|><yAA>" as this can be completely empty or have several more, such as... "<yTRS><g|><yAA><yIPP><g|><yNMS><g|><yGE>"

What I want to know is, is it possible to REGEX none to single, multiple "<yXX>'s" sepparated with "<g|>'s" ?

Code:
PATTERN: <g(\a+):> <y(\a+)><g\[><y(.*)><g]> <g\[><y(\a+)><g\|><y(\a+)><g\|><y(\a+)><g\|><y(\a+)><g\|><y(\a+)><g\]> <gSI: \[><y(.*)><g\]>
VALUE: #var CurGline1 %concat( %1, ": ", %2, " (",%3, ")  [", %4, "|", %5, "|", %6, "|", %7, "|", %8, "]  S.I.N.C. : ",%9)


thanks in advance.


Last edited by chaossdragon on Thu Sep 22, 2016 2:42 am; edited 2 times in total
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Thu Sep 22, 2016 12:44 am   
 
If I am understanding your question correctly, then I must say that REGEX can not always do this. It is not possible to have an arbitrary number of captures with a given REGEX.

That said you can have a pattern such as:
^<g\a+:> <y\a+>( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?( <g[>.*?<g]>)?<gSI:[><y[^>]*><g]>
You have to make sure you repeat the optional section enough time to cover any and all eventualities. Your script will also have to handle an equal number of situations.

The question mark after each capture makes that entire section optional, and that makes the '.*' inside the captures need to be reduced from greedy to minimal '.*?'. I couldn't see using a narrower interior wildcard after examining your example which contains letters, numbers and the percent sign.

You will have to use script to parse the captured data. You might be able to simplify this a little by using a structure more like '(?: <g[>(.*?)<g]>)?'. That makes the captured portion a little smaller and uses the grouping (?:) to indicate the optional section. You can refine it further to limit what you have to parse in script.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Thu Sep 22, 2016 12:57 am   
 
Alternately you can do the capture this way:
^<g\a+:> <y\a+>((?: <g[>.*?<g]>)*)<gSI:[><y[^>]*><g]>

The result would be a single capture that you can then pass to %subregex to break into to separate entries as needed. Subregex is capable of breaking all the data down and formatting it into whatever structure you would like.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
chaossdragon
Novice


Joined: 09 Apr 2008
Posts: 42

PostPosted: Thu Sep 22, 2016 1:02 am   
 
I suppose a few sample captures of the actual mud output might help explain things a touch more.

No multiple captures -
Code:
ND: Yes|No [0-100%] SI: [0-100.0-9999%]

Single capture -
Code:
ND: Yes|No [0-100%] [IPP] SI: [0-100.0-9999%]

Multiple capture -
Code:
ND: Yes|No [0-100%] [IPP|GE|NMS|TRS|AA] SI: [0-100.0-9999%]


** the capture limit is the first value of SI (before . ) 1(absolute) + SI / 10 +1(optional based on guild perk)
in my case at Glvl/SI 41 the capture limit is 1+4

the crap load of <g <y and > are just for color coding the value contained within the brackets to, hopefully obvious, green/yellow respectively. so no need to be captured.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Thu Sep 22, 2016 2:34 am   
 
That didn't actually clarify anything since it mostly contradicts what you had before, anyhow here is a regex for that:
ND: (?:Yes|No) [\d+%](?: [([a-zA-Z|]++)])? SI: [[0-9.]+%]

The captured portion would be "", "IFF", and "IFF|GE|NMS|TRS|AA" respectively for your examples.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
chaossdragon
Novice


Joined: 09 Apr 2008
Posts: 42

PostPosted: Thu Sep 22, 2016 2:42 am   
 
I see what you mean bu contradicts, I tried to simplify some of it from actual mud output as its supposed to be seen once the MIP code and <g <y > are removed.
The un parsed/gag'd/what have you, still looks like the original pattern. The other 3 samples are how it 'should' look. Sorry for the misunderstanding
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Thu Sep 22, 2016 11:39 am   
 
I reread your first post and noticed that I had mistaken some of the symbols. Let's try this.

Pattern: ^<g([^>]+):> <y([^>]+)><g[><y([^>]+)><g]>(?: <g[>((?:<y[^>]+>(?:<g\|>)?)++)<g]>)? <gSI: [><y([^>]+)><g]>
Value: #var CurGline1 %concat( %1, ": ", %2, " (",%3, ") [", %subregex(%4, "(?:<(?:y|g)|>)", ""), "] S.I.N.C. : ",%5)
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
chaossdragon
Novice


Joined: 09 Apr 2008
Posts: 42

PostPosted: Thu Sep 22, 2016 12:07 pm   
 
There is an unmatched parenthesis (35) Let me see if i can remove a few bits of info that really doesn't need to be captured.

Actual mud output:
Code:
<gND:> <yYes><g[><y40%><g]> <g[><yIPP><g|><yGE><g|><yNMS><g|><yTRS><g|><yAA><g]> <gSI: [><y41.3617%><g]>


How I would like it to display:
Code:
ND: %1[%2 %] [Tricky part AKA %3] S.I.N.C.: [%4 %]

**spaces in the [ ]'s values and % just for clarity of code reading...

%1 = Yes|No (guild super power)
%2 = 0-100 % (time/percent till super power recharges)
%3 will be any combination of "no output" or "ABC" or "ABC|ABC|etc|etc" (active offense, defense, repair)
%4 = ##.####% (Glvl.G2N% **not super important but nice to track)

That hopefully will simplify things more.
Reply with quote
chaossdragon
Novice


Joined: 09 Apr 2008
Posts: 42

PostPosted: Thu Sep 22, 2016 4:41 pm   
 
Update...

New Pattern:
Code:
<gND:> <y(\a+)><g\[><y(.*)><g\]> (?:<g\[>((?:<y[^>]+>(?:<g\|>)?)++)<g\]>)? <gSI: \[><y(.*)><g\]>


New Value:
Code:
#VA CurGLine1 {%concat( "ND: ", %1, "[", %2, "]  [", %subregex(%3, "(?:<(?:y|g)|>)", ""), "]  S.I.N.C.: [", %4, "]")}


Outputs:
Code:
ND: Yes[44%]  [TRS|AA|IPP|GE|NMS]  S.I.N.C.: [41.4015%]


Thanks for the help in pointing me towards the solution.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Fri Sep 23, 2016 7:53 pm   
 
Glad to help and glad you found the parenthesis mismatch.

You might want to avoid using .* in your pattern because it can lead to a very slow processing and may even cause %3 to be completely lost. I once had a complex pattern that caused a 3 minute lockup of CMud when it was matching specific data.
Use either the [^>]+ construct to match anything that is not > or use .*? to make the regex try to match the fewest characters possible for that wildcard.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
chaossdragon
Novice


Joined: 09 Apr 2008
Posts: 42

PostPosted: Fri Sep 23, 2016 10:16 pm   
 
ah that's very good news to know, was wondering why my client suddenly started to lag about a half second or so constantly.

again thank you
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
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

© 2009 Zugg Software. Hosted by Wolfpaw.net