How to backreference in Ruby regular [removed]regex) with gsub when I use grouping?

后端 未结 3 1656
花落未央
花落未央 2020-12-14 07:00

I would like to patch some text data extracted from web pages. sample:

t=\"First sentence. Second sentence.Third sentence.\"

There is no sp

3条回答
  •  时光说笑
    2020-12-14 07:33

    If you got here because of Rubocop complaining "Avoid the use of Perl-style backrefs." about $1, $2, etc... you can can do this instead:

    some_id = $1
    # or
    some_id = Regexp.last_match[1] if Regexp.last_match
    
    some_id = $5
    # or
    some_id = Regexp.last_match[5] if Regexp.last_match
    

    It'll also want you to do

    %r{//}.match(some_string)
    

    instead of

    some_string[//]
    

    Lame (Rubocop)

提交回复
热议问题