Whats wrong with this regular expression

Status
Not open for further replies.

BurningOrca

Veteran
Veteran
Joined
Apr 14, 2019
Messages
218
Reaction score
195
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,223
Reaction score
1,630
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
218
Reaction score
195
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,223
Reaction score
1,630
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
218
Reaction score
195
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
8,016
Reaction score
5,326
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

While we prepare the official trailer, enjoy this kind-of-second teaser! ^^
-Ele
New Episodes of RPG Shenanigans Uploaded to Youtube!

Episode 5 - Surprise Party!
Youtube Link:
Episode 6 - Killer Gin
Youtube Link:
Episode 7 - Gaia's Melody: Echoed Melodies
(Coming soon!)

Episode 8 - Clarent Saga: Tactics
(Coming soon!)

Episode 9 - Star Shift
(Coming soon!)
When the Map Generator throws in the assets in the most dumbest way possible - your path is blocked :D

I went to sleep at 3 am because of my anxiety. Set up my alarm for 7 am so that I could have sasagues for breakfast and do morning routine before lessons starts at 8 am. I knew I wouldn't be able to sleep even after my lessons finished because I have to visit my grandparents today I was sad bc I was really tired. Thats when I realised. My lesson starts at 9 am. I could get one extra hour of sleep if I didnt forget it

Forum statistics

Threads
107,562
Messages
1,030,570
Members
139,671
Latest member
WDRS
Top