I will focus mainly the regular expression rather than gsub since it seems you are asking for help with those rather than how gsub works.
First of all I would redirect you to a regular expressions reference. You can find one in my regex tutorial:
http://rmxp.org/forums/index.php?topic=21009.0
Alternatively you can use Rubular which is excellent for testing regular expressions:
http://www.rubular.com/
It is really gold having a reference when looking at or creating regular expressions.
Let's go through the first:
/\\V\[([0-9]+)\]/i
First notice the options, it's the
i after the last /. It means that the regular expression is case insensitive.
Now let's look at the contents of the regular expression. We can split it up to these tokens
\\ -
V -
\[ -
( -
[0-9]+ ) \]
The
\\ is an \ escaped. The escape system works by adding a backslash in front of a special character to have it considered as a normal character. In this case we escape the backslash with a backslash and the result is \\
Next is the
V which simple the V character. Since the case insensitive option is used it is actually
V or
v.
The
\[ is like the
\\ with you having a special character you need to escape. So that bit is treated as the character
[.
The
( is a bit special since what it does together with
) is that it marks and area of the regular expression. It groups a part of the regular expression however the grouping features are not used. I will come back to how this is used, just know that it does not change which strings will match the regular expression in this case.
The
[0-9] bit of
[0-9]+ is one digit. The special thing about
[something] is that it represents one character which can be any one of the character within the square brackets. This is also the reason why we had to escape the brackets before. Because we wanted to look for the
[ character in the string. You could also have used
\d instead for matching a digit. It's a matter of taste really. Note that
\d and
\D is different despite the case insensitive option.
The + means at least one. Basically the text has to have at least one digit. It can have any number of digits as long as there are at least one.
Finally we have
\] which is an escaped end bracket. So we look for the
] character.
Putting it all together we have a regular expression matching a any string that has \V[
digits] in it somewhere. (It does not have to be a capital v)
The gsub method then finds any part in the string which matches that and replaces it with something else. Now we come into the use of the parentheses. $1 is set to what the first group matches, $2 to what the second group matches and so on.
For example, if you have \v[23] then the part in () is the digits and thus $1 is set to "23".
So the \v[23] is replaced with whatever $game_variables[23] returns. It returns an integer, but it is automatically converted to a string. By the to_s method I am pretty sure but I have not tested.
I see that the next line does the same, but there won't be anymore matches of that particular regular expression since they all were removed last time. (Well assuming that all variables contain numbers)
You can just as well delete the duplicate line since it won't do anything except consuming cpu cycles in all but the strangest of situations.
/\\N\[([0-9]+)\]/i and
/\\C\[([0-9]+)\]/i work similar with
V being exchanged with
N and
C respectively. Look at how the number part are used in the substitution. ($1)
\\G looks for \G in the text. It is not case insensitive so it will not match \g. Just a little thing to notice.
\\\. is \ and . both escaped. The . is a special character that can match any character. When escaped it's simple the . we are looking for. So it will match \. in the text.
\\\| will match \|. The special character | is used as
or. For example a|b matches a or b. We escape it since it's a special character
\\!,
\\> and
\\< will match \!, \> and \< respectively. Notice that ! is not a special character and thus we have no need to escape it. Likewise with > and <.
\\\^ will match \^. The ^ special character can be used inside []'s. For example [^something] will match any character but the characters specified inside. I. e. all characters but e,g,h,i,m,n,o,s,t.
\\\\ will match \\. We need to escape both of the \'s thus giving us \\\\.
If you need more help feel free to ask.
*hugs*
- Zeriab