zorrox
zorrox

Reputation: 23

how I could regex ruby replace by group 10 using gsub?

Im trying to gsub replace a string using a group 10 but instead of this ruby replace by group 1 and 0 I use this in gsub replacement

gsub   \\10xp
  "smnm nmnmn nmnmn dsdsf sffddf sffdfd dfsff fdsdsfd fsdds ssfsff".gsub(/(\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+)/,"\\10xp")  

for replace and obtain the group 1 and the string "0xp"

How I could solve this

Please help me

Upvotes: 2

Views: 279

Answers (2)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

No one known regex engine does support unnamed groups beyond [0..9] region. Use named_captures instead, or use uncaptured groups (?:...), or use wiser regex:

"smnm nmnmn nmnmn dsdsf sffddf sffdfd dfsff fdsdsfd fsdds ssfsff".
  gsub(/((?:\S+\s+){9})(\S+)/, "\\1 \\2xp")

#                      ⇑ second group
#                  ⇑⇑⇑ nine times
#        ⇑⇑⇑ don’t capture this!
#       ⇑ first group

Upvotes: 2

Simple Lime
Simple Lime

Reputation: 11035

Not sure how to reference a double digit capture group, but you can always just reference a name with something like:

"smnm nmnmn nmnmn dsdsf sffddf sffdfd dfsff fdsdsfd fsdds ssfsff".
  gsub(/(\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (?<ten>\S+)/,
       '\k<ten>xp')
# "ssfsffxp"

Upvotes: 2

Related Questions