Actions
Bug #18271
closedRegexp inconsistency (repeatable)
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
Description
han:~/au> irb >> st = 'A. C. Cobble'
=> "A. C. Cobble"
st.gsub(/. ([A-Z])(?=.)/, ".#{$1}")
=> "A.. Cobble"
st.gsub(/. ([A-Z])(?=.)/, ".#{$1}")
=> "A.C. Cobble"
RUBY_VERSION
=> "3.0.2"
Updated by zverok (Victor Shepelev) over 2 years ago
$1
is global variable, "#{$1}"
is evaluated BEFORE the method call, making a string "whatever was in $1
at that moment":
st = 'A. C. Cobble'
'foobar' =~ /f(.{2})/ # unrelated regex call, sets $1 to "oo"
st.gsub(/. ([A-Z])(?=.)/, ".#{$1}") # it is now an equivalent to calling with second arg = ".oo"
# => "A.oo.ooobble"
If I am understanding correctly what you want to achieve, you need to use \1
(meaning "first group of currently evaluating regexp"):
st.gsub(/. ([A-Z])(?=.)/, '.\1')
# => "A.C.Cobble"
Updated by jeremyevans0 (Jeremy Evans) over 2 years ago
- Status changed from Open to Closed
Actions
Like0
Like0Like0