Aug 12, 2014
personal note 5 : Reading RegEx
(1) What happened
I had trouble at RegEx while editing my CC_FG.
In line 300 in Window_Base;
#--------------------------------------------------------------------------
# * Destructively Get Control Code Argument
#--------------------------------------------------------------------------
def obtain_escape_param(text)
text.slice!(/^\[\d+\]/)[/\d+/].to_i rescue 0
end
What is that [/\d+/] ?
That single line crashed my whole night.
Perhaps I'll forget what does this line say, so leaving this note.
(2) Decryption
That bracketed RegEx ([/\d+/]) is one style of [] for String.
It returns a matched part of String.
Example:
"hoge01"[0] => "h"
"hoge01"[/\d+/] => "01"
so a collapsed form of the line means;
begin
str = text.slice!(/^\[\d+\]/)
str.match(/\d+/)
str = $&.to_i
rescue
0
end
(3) Application
And from there, I made a line to get String parameter.
str = text.slice!(/^\[.+?\]/)
str.match(/\[(.+?)\]/)
str = $1
or
str.match(/\[(.+?)\]/)[1]
or
text.slice!(/^\[\S+\]/)[/\[(.+?)\]/, 1]
I will not use the compressed RegEx line though.
(4) Biproduct
Class Window_Base < Window
def obtain_escape_param_string(text)
text.slice!(/^\[\S+\]/)[/\[(.+?)\]/, 1] rescue 0
end
end
This allows us to take a parameter as String for control characters in RMVXA.
I thought I need this method in CC_FG at first, but not in result.
('w')
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment