Whats wrong with this regular expression

Status
Not open for further replies.

BurningOrca

Veteran
Veteran
Joined
Apr 14, 2019
Messages
214
Reaction score
187
First Language
German
Primarily Uses
RMMV
Code:
var test = Window_Base.prototype.convertEscapeCharacters
Window_Base.prototype.convertEscapeCharacters = function(text)
{
      text = test.call(this, text);     
      text = text.replace(/\x1bPPIC\<(\d+)\s*,\s*(.+)\>(\{(Scale:\[([-]?\d+){1},([-]?\d+){1}\])?([,]?\s*Opacity:(\d+){1}?)\})?/gi, function()
      {
              var scale = {x:150,y:150};
              var opacity = 255;
              if( arguments[3] )
              {
                  if( arguments[4] )
                  {
                      scale.x = Number(arguments[5]);
                      scale.y = Number(arguments[6]);
                  }
                  if( arguments[7] )
                  {
                      opacity = Number(arguments[8]);
                  }
              }
        // Do something here
        return '';
     }.bind(this));

}
The second show text event is the one which is not working. arguments[3] is not filled for this one and I don't know why?
upload_2019-10-29_9-15-30.png
1st returns Oh this is a tree!!!
2nd returns {Scale:[125,-125]}Oh this is a tree!!!
3rd returns Oh this is a tree!!!
4th returns Oh this is a tree!!!

How can I make the second one returns also only Oh this is a tree!!!
 

Attachments

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,091
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
Oh wow that's a convoluted escape sequence... :kaoback:

Well, I tested your RegExp against the test string in the console, and sure enough it only matches "\x1bppic<1,Child>"~
Code:
/\x1bPPIC\<(\d+)\s*,\s*(.+)\>(\{(Scale:\[([-]?\d+){1},([-]?\d+){1}\])?([,]?\s*Opacity:(\d+){1}?)\})?/gi.exec('\x1bppic<1,Child>{Scale:[125,-125]}Oh this is a tree!');
I couldn't spot any obvious mistakes, so rather than try to decipher what's gone wrong, I rewrote it from scratch. This seems to produce the expected matches for me from the test string; assuming it works for you, maybe you can use it to help figure out what was wrong with your expression? :kaoswt:
  • Without spacing:
    Code:
    /\x1bPPIC\<(\d+),(.+?)\>(\{(Scale:\[([-]?\d+),([-]?\d+)\])?[,]?(Opacity:(\d+))?\})?/gi
  • With spacing:
    Code:
    /\x1bPPIC\<(\d+)\s*,\s*(.+?)\>(\{(Scale:\[([-]?\d+)\s*,\s*([-]?\d+)\])?[,]?(\s*Opacity:\s*(\d+))?\})?/gi
 

BurningOrca

Veteran
Veteran
Joined
Apr 14, 2019
Messages
214
Reaction score
187
First Language
German
Primarily Uses
RMMV
Thank you very much, it works perfect.
So I know "." means any character, "+" means 0 or more and "?" means 0 or 1, but could you tell me what does the combination ".+?" do?
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,091
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
Great! :kaojoy:

By default, regular expressions use a "greedy" algorithm, i.e. they make the longest possible match fitting the expression. Adding a ? immediately after a quantifier (e.g. * or +) forces that match to be "non-greedy", i.e. the shortest possible match fitting the expression. Details can be found here~ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Quantifiers

I've used the non-greedy match here because you could have stuff like this in your message:
Code:
\ppic<1,NPC>Hey, is that...\>\P[0]\<!?
In this case a greedy match might see the separate \> escape character as part of your text match, yielding:
Code:
NPC>Hey, is that...\
...instead of the expected match: NPC. There are other ways around this issue, though, e.g. use an excluded character group instead of a dot: [^>]+. Hope that makes sense! :kaothx:
 

BurningOrca

Veteran
Veteran
Joined
Apr 14, 2019
Messages
214
Reaction score
187
First Language
German
Primarily Uses
RMMV
Thank you very much.
This information was very useful and I've learned a lot.
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
7,842
Reaction score
5,225
First Language
Dutch
Primarily Uses
RMXP

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,085
Members
137,584
Latest member
Faustus2501
Top