简体   繁体   中英

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

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM