Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

[Scripting] Regular Expressions

Status
Not open for further replies.

LTN Games

Master Mind
Resource Team
Xy$
0.01
I am having a hell of a time getting my regular expression to match properly, my first regular expression worked well as far as I know, but now that I need to account for a double-digit number, every time I get the output of the match it's only a piece of what I am trying to capture.

First of all my function to capture the groups is here. This function will go through the string, and it will find the Message codes so that I can center my text properly.

My regular expression works fine in this online tester
https://regex101.com/r/mU5jY5/3

JavaScript:
Window_Pop.prototype.adjustStringWidth = function(string) {
    var widthToRemove = '';
    var toDelete = string.match(/([\/\C\N\V\G\I\][\d*])/g);
    console.log(toDelete);
    if(!toDelete) return 0;
    for (var i = 0 ; i < toDelete.length ; i++) {
      widthToRemove += toDelete[i];
    }
    console.log('widthToRemove = ' + widthToRemove);
    var removedWidth = this.textWidth(widthToRemove);
    return removedWidth;
  };
The console will output this string, for some reason it won't find the / but replaces it with a C and another strange thing is it's adding a C to the end of the string.
Code:
 C[14]C
This is what it should look like.
Code:
 \C[14]
Any help or suggestion would be much appreciated, regular expressions are tough to create when you are just learning about them. Thanks in advanced :)
 

JibstaMan

Towns Guard
Xy$
0.00
I'm not entirely sure what you're trying to do here (and maybe that's on purpose?).

If you want to capture \C[14], this a regular expression that can do that:
Code:
/(\\[CNVGI]\[\d*\])/g
Your regex has \/, which captures a forward slash, instead of a backslash. Your regex101 example also has forward slashes in the string, which you don't mention in your description. Since I guess text codes are still using backslashes due to my experience with VX, I assume your description described what you require. ;)

Explanation:

It only has one capture group, so it either finds the entire thing, or nothing.

A double backslash within the regex searches for a single \ literally. The backslash is necessary, so I moved it out of the character group ([]).

I've also moved the [ and ] characters out of the character group, since those are also necessary.

Removed all the backslashes within the character group, since those aren't necessary as well.

Removed the [] surrounding the digit matcher; it is redundant since we're not capturing any other character.

When I say necessary, I mean anything that doesn't contain that character at that position within the string shouldn't be matched. \C[14] should be matched, but \C14] shouldn't be, right?

Hope this helps! Either way, good start! Regular expression is indeed quite a tough topic.
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
If you want to capture \C[14], this a regular expression that can do that:
Ahh thank you, this works like a charm and is exactly what I'm looking to accomplish. I find Regular expression really tough to understand at the beginning, just because it's like it's own language inside of another language lol. You explained this very well, it's exactly what I need to hear because I was starting to go crazy lol. I'm pretty sure I understand it pretty good now with your explanation so I should be good to go.
I'm not entirely sure what you're trying to do here (and maybe that's on purpose?).
Basically, all this function does is go through the string, find the message codes, calculates the width of the message codes, then I take that info in another function to adjust my x value according to the alignment set, left, right or center.

Btw according to the regex tester it does not capture anything in the string, but according to my plugin it works like a charm, I think it will be wise of me not to use this specific reg ex tester lol. Thanks again I really appreciate the help.
 

JibstaMan

Towns Guard
Xy$
0.00
When I want to test a regex, I start with using regex101, then (if necessary) move to jsfiddle to test it outside of the entire game. I believe regex101 isn't the problem, but your test string is (it contains forward slashed, not backward slashes).

Regex:
Code:
/(\\[CNVGI]\[\d*\])/g
Test string:
Code:
\C[2] \C[2]d ads \C[22] color one tow \V[3] \N[2] \G[1] and then we all went along
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
I believe regex101 isn't the problem, but your test string is (it contains forward slashed, not backward slashes).
Omg, hahaha note to self, don't work on reg-ex 2am in the morning, This explains alot, I can't believe I've never noticed that before. Thank you for pointing that out I would have never noticed. Things probably would have went smoother if I noticed my test string was incorrect, because I kept trying numerous expressions with that exact string trying to figure out why I was not working like I had it before. Thanks again you're a great help.
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
I'm starting to get the hang of it, which is nice, but when your first learning it,you only learn a tiny bit at a time, and this makes it difficult to make a proper expression. I would never have learned about it without the reg ex tester though, I just kept trying to make random expressions and from doing this I learned bits and pieces at a time. So if you want to learn it, your best bet is playing with the reg ex tester.
 

JibstaMan

Towns Guard
Xy$
0.00
So you need the width of the message codes? Might I give a suggestion:

Code:
Window_Pop.prototype.adjustStringWidth=function(string){
   var widthWithCodes = string.length;
   var decoded = string.replace(regex, '');
   return widthWithCodes -  decoded.length;
};
Not sure this is useful, but figured I let you be the judge of that. I'm starting to get curious about the this.textWidth function (tongue)
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
Ah yes, my setup seems to be working really well for me, but this seems to be much smaller than mine, I'll play around with this and see if it produces the same result and if it is I'll probably shrink my function some, the smaller the better.

The this.textWidth function is a function packed with MV's Window_Base class, it calls another function in the core.js called measureTextWidth and since it's there I might as well us it lol

this is the Core.js measureTextWidth function.
JavaScript:
/**
* Returns the width of the specified text.
*
* @method measureTextWidth
* @param {String} text The text to be measured
* @return {Number} The width of the text in pixels
*/
Bitmap.prototype.measureTextWidth = function(text) {
    var context = this._context;
    context.save();
    context.font = this._makeFontNameText();
    var width = context.measureText(text).width;
    context.restore();
    return width;
};
 
Status
Not open for further replies.
Top