Project

General

Profile

Misc #14500

Missing Regexp documentation and clarification on behavior of \K for edge case

Added by Sundeep (Sundeep Agarwal) about 1 year ago. Updated 12 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
[ruby-core:85707]

Description

Capturing section (https://ruby-doc.org/core-2.5.0/Regexp.html#class-Regexp-label-Capturing)

Anchors section (https://ruby-doc.org/core-2.5.0/Regexp.html#class-Regexp-label-Anchors)

  • suggestion to add documentation on \K
  • need clarification if the below behavior seen is expected and mention it while adding documentation
$ echo 'aaa' | ruby -pe 'gsub(/a\K/, ":")'
a:aa:

$ # what I expected
$ echo 'aaa' | ruby -pe 'gsub(/(a)/, "\\1:")'
a:a:a:

History

Updated by znz (Kazuhiro NISHIYAMA) 12 months ago

Sundeep (Sundeep Agarwal) wrote:

  • suggestion to add that numbered capturing groups is limited to 9

I don't think so.
Why did you think so?

irb -r irb/completion --simple-prompt
>> /()()()()()()()()()(a)/ =~ "a"
=> 0
>> $10
=> "a"

Updated by Sundeep (Sundeep Agarwal) 12 months ago

oh, I didn't check with $10. I'd tried with backreference. Any idea how to use \10?

$ echo 'abcdefghij' | ruby -pe 'sub(/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/, ":\\9:")'
:i:
$ echo 'abcdefghij' | ruby -pe 'sub(/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/, ":\\10:")'
:a0:
$ echo 'abcdefghij' | ruby -pe 'sub(/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/){":#{$10}:"}'
:j:

Also available in: Atom PDF