MZ RegExp and the match function, little help please & thank you

JavaScript:
let text = "yay! again! \SW[1>true]...\!\SW[1>false] YAY!";
let pattern = /SW\[(\S+)\]/gi;
let result = text.match(pattern);

With that result returns:
SW[1>true]...!SW[1>false]

Any helpful advice on how can I get it to return the array of:
SW[1>true]
SW[1>false]
I'm sure it's something simple I just am not very familiar with using RegExp and the match function.


Thanks in advance for any help ☺️
Do you want it to look like this?
1648169629159.png


If so, use this for your regex
JavaScript:
/SW\[(\d>\w+)\]/gi

**edit, this will only work if the format is always the same btw
Do you want it to look like this?
View attachment 220922

If so, use this for your regex
JavaScript:
/SW\[(\d>\w+)\]/gi

**edit, this will only work if the format is always the same btw
Awesome! You rock! Thank you so much! ☺️ 👍
(.. can't believe I didn't think of that though lol)

Although...
I had kind of wanted to use both sides as things that could be javascript to later be evaluated
yay! again! \SW[$gameVariables.value(1)>true]...\!\SW[1>0] YAY!

Happen to know of any way to make something like this work?
lol... i know that might be pushing my luck now though :blush:

Edit:
I did actually get the results I was looking to achieve by doing this:
JavaScript:
this.switchData = [];
let pattern = /SW\[(\S+)\]/gi;
let result = text.match(pattern);
result.forEach(function(v){
    var data = v.split(']');
    data.forEach(function(v2){
        let newPattern = /SW\[(\S+)/gi;
        let newResult = v2.match(newPattern);
        if (newResult){
        newResult.forEach(function(v3){
            var d = v3.replace('SW[','').split('>');
            this.switchData.push({id: d[0], on: d[1]});
        },this);
        }
    }, this);
}, this);
..but maybe there is still an easier way?
then after that to replace the message box escape codes I did the following...
JavaScript:
        text = text.replace(/\x1bSW\[(\d+>\d+)\]/gi, (_, p1) =>
            "\x1b\SW"
        );
        text = text.replace(/\x1bSW\[(\w+>\d+)\]/gi, (_, p1) =>
            "\x1b\SW"
        );
        text = text.replace(/\x1bSW\[(\w+>\w+)\]/gi, (_, p1) =>
            "\x1b\SW"
        );
        text = text.replace(/\x1bSW\[(\d+>\w+)\]/gi, (_, p1) =>
            "\x1b\SW"
        );
        text = text.replace(/\x1bSW\[(\S+>\S+)\]/gi, (_, p1) =>
            "\x1b\SW"
        );
all that does work as I need it to... seems if I remove any of them though it does break.. not really sure why though
You could probably change the pattern to
SW\[(\d+)>(\w+)\]
and get both sides of the result as parts of the array, so you can do JSON.parse() on them later.
You could probably change the pattern to
SW\[(\d+)>(\w+)\]
and get both sides of the result as parts of the array, so you can do JSON.parse() on them later.
Much appreciated for the reply ☺️


would that work for something like this?
yay! again! \SW[$gameVariables.value(1)>true]...\!\SW[1>0] YAY!
Whew! That was quite the challenge but I think I got it?
Code:
(\w+)\[((\d+)*(\w+)*)|(\$(\w+)\.(\w+)\((\d+)\))?>(\d+)*(\w+)*\]

Also, this is a fabulous regex tool.
Bestest regex tool ever

1648172091711.png
the d+ part of the match is looking for number(s). $gameVariables.value(1) is a string when you're checking it so isn't what the match is looking for. you could just call $gameVariables.value(1) and convert the result to a string since you're using javascript at the moment, something like this:
let text = "yay! again! \SW[" + $gameVariables.value(1) + ">true]...\!\SW[1>0] YAY!";
notice that we excluded $gameVariables.value(1) from the string and had to use plus signs to concatenate it (basically, check the actual value and add the result into the string).
☺️ the d+ part of the match is looking for number(s). $gameVariables.value(1) is a string when you're checking it so isn't what the match is looking for. you could just call $gameVariables.value(1) and convert the result to a string since you're using javascript at the moment, something like this:
let text = "yay! again! \SW[" + $gameVariables.value(1) + ">true]...\!\SW[1>0] YAY!";
notice that we excluded $gameVariables.value(1) from the string and had to use plus signs to concatenate it (basically, check the actual value and add the result into the string).
Yeah sounds good 🙂 I get whatcha mean.. the issue with doing it that way is the first part is going to be a javascript evaluated code that ends up being a switchID and the second part is javascript that determines whether or not it is set to "on" or "off"
I think Tea might be on to something.. looks like she got it :popcorn:

Thanks again for all the advice ☺️

Whew! That was quite the challenge but I think I got it?
Yay! Thanks for making me feel better for not being able to crack the code
At least I know it wasn't just something simple and was actually a challenge

Whew! That was quite the challenge but I think I got it?
Code:
(\w+)\[((\d+)*(\w+)*)|(\$(\w+)\.(\w+)\((\d+)\))?>(\d+)*(\w+)*\]

Also, this is a fabulous regex tool.
Bestest regex tool ever

View attachment 220926
OMG! Wow! That is amazing! 😮☺️👍
This all could definitely prove quite useful.

lol i see it does even my other messagebox escapecodes too...
sooo what would I add to this to just get the "\SW" ones?

PS. Thank you so much for the link to that site. That's gonna save me so much in the future. Playing around with it now.
You should be able to just replace (\w+) at the beginning with SW. Give that a try.
You should be able to just replace (\w+) at the beginning with SW. Give that a try.
That's what I thought too... but it does this...

1648173486205.png


Am I doing something wrong?
Nope, I forgot that it should be a group. Can you copy and paste your test text in so I can grab it?
Nope, I forgot that it should be a group. Can you copy and paste your test text in so I can grab it?

Absolutely here ya go 🙂
poof!\!\CP[999>Evil_5] yay!\CP[999>Evil_3] again!\CP[999>Evil_6] & animate!\! \SW[$gameVariables.value(1)>true]...\!\SW[1>false] YAY!\! ..again ...\SW[$gameVariables.value(1)>1]

Thanks again!
Maybe this?
Code:
(SW\[((\d+)*(\w+)*)|(\$(\w+)\.(\w+)\((\d+)\))>((\d+)*(\w+)*\])?)
Maybe this?
Code:
(SW\[((\d+)*(\w+)*)|(\$(\w+)\.(\w+)\((\d+)\))>((\d+)*(\w+)*\])?)

OMG sooooo close
1648174462813.png


but looks like a slight issue still if the it's something else besides for "$gameVariables.value(x)" used after the "\SW[" then it doesn't get the whole thing.
...basically the real goal is to get it to where both sides of the ">" can be any sort of javascript
(just going to remove the "SW" and the brackets then split the returned string with ">")

Edit:
Can you use this as the test text:
poof!\!\CP[999>Evil_5] yay!\CP[999>Evil_3] again!\CP[999>Evil_6] & animate!\! \SW[$gameVariables.value(1)>$gameVariables.value(2)]...\!\SW[1>false] YAY!\! ..again ...\SW[$gameVariables.value(1)>1] & this... \SW[$gameVariables.value(1)>this.customCode()]

I almost got it all working but mine looks like this now..
1648175303872.png

This is what i used to do that.. if helps at all...
JavaScript:
(SW\[((\d+)*(\w+)*)|(\$(\w+)\.(\w+)\((\d+)\))?>((\d+)*(\w+)*)|(\$(\w+)\.(\w+)\((\d+)\)*])?)
Ooops, I didn't notice that missing bit. Maaayyybeee this?
JavaScript:
(SW\[(\$(\w+)\.(\w+)\((\d+)\))*((\d+)*(\w+)*)>((\d+)*(\w+)*\])?)
:LZSlol:
Ooops, I didn't notice that missing bit. Maaayyybeee this?
JavaScript:
(SW\[(\$(\w+)\.(\w+)\((\d+)\))*((\d+)*(\w+)*)>((\d+)*(\w+)*\])?)
:LZSlol:

Yep perfect ☺️:LZScheeze:☺️
using the test text I gave you before that is 100% perfect
...but now what if I use something like this:

poof!\!\CP[999>Evil_5] yay!\CP[999>Evil_3] again!\CP[999>Evil_6] & animate!\! \SW[$gameVariables.value(1)>$gameVariables.value(2)]...\!\SW[1>false] YAY!\! ..again ...\SW[$gameVariables.value(1)>1] & this... \SW[$gameVariables.value(1)>this.customCode()]

I'm so sorry to keep throwing things at you like this..
just wanna be sure whatever the user puts in the message box escapecode text can be javascript evaluated as needed.

with the latest bit you gave me it looks like this:
1648175759297.png


Edit:
poof!\!\CP[999>Evil_5] yay!\CP[999>Evil_3] again!\CP[999>Evil_6] & animate!\! \SW[this.customCode()>$gameVariables.value(2)]...\!\SW[1>false] YAY!\! ..again ...\SW[$gameVariables.value(1)>1] & this... \SW[$gameVariables.value(1)>this.customCode()]
This seems to be a real good test text to make sure, seems like it covers all examples of all that could be entered.
Yep, this one has me stumped, not sure why it won't grab the one on the second line. I added multiline but it still wouldn't touch it.
regexr.com/6i5bl

Here's a link to the regexer page with my attempt.

Of course I figure it out right after posting that I give up lol
JavaScript:
(SW\[(\$(\w+)\.(\w+)\((\d+)\))*((\d+)*(\w+))*>((\d+)*(\w+)*)*((\$)*(\w+)\.(\w+)(\(((\d+)*)\)))*\])
Yep, this one has me stumped, not sure why it won't grab the one on the second line. I added multiline but it still wouldn't touch it.
regexr.com/6i5bl

Here's a link to the regexer page with my attempt.

Of course I figure it out right after posting that I give up lol
JavaScript:
(SW\[(\$(\w+)\.(\w+)\((\d+)\))*((\d+)*(\w+))*>((\d+)*(\w+)*)*((\$)*(\w+)\.(\w+)(\(((\d+)*)\)))*\])
lol yep that's about how it goes huh lmao..

awesome! this looks great! ☺️

.. ... ...

...except it doesn't quite get all
please don't be mad *runs and hides* lmao

1648177940722.png


Makes me feel better this is so dang tricky to solve though.

Latest test text:
poof!\!\CP[999>Evil_5] yay!\CP[999>Evil_3] again!\CP[999>Evil_6] & animate!\! \SW[customCode()>$gameVariables.value(2)]...\!\SW[1>false] YAY!\! ..again ...\SW[$gameVariables.value(1)>1] & this... \SW[$gameVariables.value(1)>this.customCode()]

Thank you again for doing all this. :LZSgrin:
You definitely have a better grasp of RegExp then I do.
Haha, thanks, but my grasp is elementary at best. My solutions are rigid like a stick, there are people out there can can write short, fluid, much more robust solutions. This should cover the newest case.
JavaScript:
(SW\[((\$(\w+)\.)*(\w+)\((\d+)*\))*((\d+)*(\w+))*>((\d+)*(\w+)*)*((\$)*(\w+)\.(\w+)(\(((\d+)*)\)))*\])

I'm enjoying this though, because it's giving me a better understanding of Regex :LZSjoy:
I'm enjoying this though, because it's giving me a better understanding of Regex :LZSjoy:
Awww yay! That's wonderful to hear ☺️
Haha, thanks, but my grasp is elementary at best. My solutions are rigid like a stick, there are people out there can can write short, fluid, much more robust solutions. This should cover the newest case.
JavaScript:
(SW\[((\$(\w+)\.)*(\w+)\((\d+)*\))*((\d+)*(\w+))*>((\d+)*(\w+)*)*((\$)*(\w+)\.(\w+)(\(((\d+)*)\)))*\])
lol awww rigid stick or not you rock!

PERFECT!!! This seems absolutely rock solid now 🙂 Exactly as I need it.
1648178513256.png

THANK YOU!!!!!!

This looks great! & You've really helped me learn more about the crazy world of RegExp too
I think this is exactly what I needed for real. I really really really appreciate ya @Tea! :LZScheeze::LZSproud:

☺️ Again many many thanks ☺️

Original thread: https://forums.rpgmakerweb.com/threads/regexp-and-the-match-function-little-help-please-thank-you.146094/