Ruby regex - gsub only captured group

后端 未结 6 823
夕颜
夕颜 2021-01-03 22:47

I\'m not quite sure I understand how non-capturing groups work. I am looking for a regex to produce this result: 5.214. I thought the regex below would work, bu

6条回答
  •  余生分开走
    2021-01-03 23:07

    You can't. gsub replaces the entire match; it does not do anything with the captured groups. It will not make any difference whether the groups are captured or not.

    In order to achieve the result, you need to use lookbehind and lookahead.

    "5,214".gsub(/(?<=\d),(?=\d)/, '.')
    

提交回复
热议问题