Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference 10th submatch [duplicate]

Tags:

regex

sed

Normally when you call sed, you can call \1 to reference the submatch number one, i.e.:

<something> | sed "s/<regex>\(<regex>\)<regex>/\1/"

This will just take the middle regex and remove the outer ones.

But what do you do, if you have more then 9 matches? Simply writing \10 doesn't work, because it will be interpreted as take submatch number one and add a zero behind it.

like image 201
erikbstack Avatar asked Feb 20 '23 14:02

erikbstack


2 Answers

It's not possible in sed. You'll have to use a different tool.

Here are similar questions on SO:

Circumvent the sed backreference limit \1 through \9.

how to get 10th grouping value in sed?

like image 148
slackwing Avatar answered Feb 27 '23 17:02

slackwing


I'm not sure about SED, but in many languages (javascript, ruby), that have regex implementations that allow for submatching (or backreferencing as it is sometimes called), only support \1 through \9. \10 is simply not allowed.

Any additional backreferencing would require the use of a more sophisticated parser.

like image 21
Hans Z Avatar answered Feb 27 '23 18:02

Hans Z