Regular expression issue - not picking up "any" value

I have the following two lines in a notebox on a state (for my state add/remove commands script):

onadd: mp -= 5onremove: mp += 5and these regular expressions to pull out the notes:
Code:
when /onadd:\s*(.*)/iwhen /onremove:\s*(.*)/i
The onadd line is being read correctly, and mp reduced as it should be. But the onremove line is only being half read.Adding some extra logging statements to the code shows that everything from the + onwards is being ignored.

$data_states[state_id].note.split(/[\r\n+]/).each do |line| case line when /onadd:\s*(.*)/i p line # >> onadd: self.mp -= 5 p $1 # >> self.mp -= 5 eval($1) when /onremove:\s*(.*)/i p line # >> onremove: self.mp p $1 # >> self.mp eval($1) endendCan someone tell me:

1. WHY the += 5 is being excluded from the second test?

2. What I need to do to make it work?
I tried this simple snippet on script editor

"onremove: mp += 5" =~ /onremove:\s*(.*)/iputs $1.to_sAnd it works fine

Alternatively, you could try put it inside <>

For example :

<onremove: mp += 5>

Not sure, but at least, try that out
That didn't work for me at all either. I still got self.mp out of self.mp += 5 in the note box.


Are you SURE it gave you mp += 5 when you ran it?


Even putting the <> around it (which I HAD done with regular expressions in the past, but am trying to avoid now) didn't pick it up.
Here is the debug console

ZwdCnen.png


And here how I set it up

jYWuz2Z.png


Edit :

Another possible approach is using this way

<onremove>mp += 5</onremove>I'm sure that you already know how to read them up 🙂
Ok, it works when you do it like that in the script editor, but not in notes in the state, which is where it needs to be.

I've narrowed it down to this:

$data_states[state_id].note.split(/[\r\n+]/).each do |line|The [\r\n+] I THOUGHT was to say "one or more". But apparently it also splits on the +.If I do this:

$data_states[26].note.split(/[\r\n+]/).each do |text|puts texttext =~ /onremove:\s*(.*)/iputs $1.to_sendit gives me this:
Code:
onremove: self.mpself.mp=5
So maybe I'm misunderstanding what the + is for in [\r\n+]?
Ah, it should be

/[\r\n]+/

I missed that
To follow up with the why

[ ] is a grouping which matches any one of the characters in the set. You had specified three characters:

  • \r - Carriage return
  • \n - Newlline
  • + - The plus character.

As TheoAllen correctly points out within grouping + not treated as a Kleene Plus operator.

*hugs*

 - Zeriab
As expected from our (legendary) Zeriab xD (I'm exaggerating).

Thanks for the explanation, since I didn't know how to explain it in English
That did actually cross my mind. The reason I had it inside the brackets was because I copied it from someone else. I THINK I've been using it that way in a lot of my scripts.


Perhaps I miscopied somewhere along the line.


Thank you both. Given my apparent typo, it's very clear now why it was doing what it was doing, and that my understanding of + was correct, after all 🙂


ps. No, you're not exaggerating. Zeriab is a legend. For many reasons 😉

Original thread: https://forums.rpgmakerweb.com/threads/regular-expression-issue-not-picking-up-any-value.27326/