Joel P. wrote in post #1125187:
irb(main):003:0> s1.gsub /(:[a-z]+)\d+/i, ‘\1moose’
=> “A1:BmoosesomestringB23”
irb(main):004:0> s2.gsub /(:[a-z]+)\d+/i, ‘\1moose’
=> “somB12estring A1:Bmoose”
hi Joel P.,
I have some doubts from the above regular expression, I don’t understand
the meaning of /i and \1. why have you added that?
Test it on irb or read the info on Regexp on www.rubular.com
/i makes the match case-insensitive, so I don’t have to use [a-zA-Z]
\1 refers to the last successful match, which in that case was “:B”
On 10/24/2013 06:44 AM, Raja gopalan wrote:
Joel P. wrote in post #1125187:
irb(main):003:0> s1.gsub /(:[a-z]+)\d+/i, ‘\1moose’
=> “A1:BmoosesomestringB23”
irb(main):004:0> s2.gsub /(:[a-z]+)\d+/i, ‘\1moose’
=> “somB12estring A1:Bmoose”
hi Joel P.,
I have some doubts from the above regular expression, I don’t understand
the meaning of /i and \1. why have you added that?
\1 in regular expressions traditionally is used to reference the string
captured from within parentheses of a pattern. In Your case that is
whatever was gotten by
(:[a-z]+). This is also often referenced by $1, though only \1 works in
older versions of regex, like that in sed. I found an example of this
on page 75 of Pickaxe second edition 1.8.
On 10/24/2013 06:44 AM, Raja gopalan wrote:
Joel P. wrote in post #1125187:
irb(main):003:0> s1.gsub /(:[a-z]+)\d+/i, ‘\1moose’
=> “A1:BmoosesomestringB23”
irb(main):004:0> s2.gsub /(:[a-z]+)\d+/i, ‘\1moose’
=> “somB12estring A1:Bmoose”
hi Joel P.,
I have some doubts from the above regular expression, I don’t understand
the meaning of /i and \1. why have you added that?
Looks like the options are described on page 324 of same Pickaxe 2nd
edition: “Regular Expression Options”:
i Case insensitive
o Substitute once
m multiline mode
x extended mode
xc
Am 24.10.2013 15:57, schrieb Xeno C.:
I have some doubts from the above regular expression, I don’t understand
the meaning of /i and \1. why have you added that?
Looks like the options are described on page 324 of same Pickaxe 2nd
edition: “Regular Expression Options”:
These can all be found in the official documentation, too:
Class: Regexp (Ruby 2.0.0)
Regards,
Marcus
On Thu, Oct 24, 2013 at 3:52 PM, Xeno C.
[email protected] wrote:
hi Joel P.,
I have some doubts from the above regular expression, I don’t understand
the meaning of /i and \1. why have you added that?
\1 in regular expressions traditionally is used to reference the string
captured from within parentheses of a pattern. In Your case that is
whatever was gotten by
(:[a-z]+). This is also often referenced by $1, though only \1 works in
older versions of regex, like that in sed. I found an example of this on
page 75 of Pickaxe second edition 1.8.
Where does $1 work in Ruby? I can only think of the block form:
irb(main):001:0> “foo”.gsub /f(o+)/, ‘<$1>’
=> “<$1>”
irb(main):002:0> “foo”.gsub(/f(o+)/) {“<#$1>”}
=> “”
It does not work with the replacement string form. Or did that change in
Ruby 2?
Kind regards
robert