quickshiftin
quickshiftin

Reputation: 69721

can't grock simple sed backreference

I'm trying to use sed to use a backreference as a replacement.

echo MXN-PKG-0.3.6-62.noarch.rpm | sed -E 's/(\d\.\d\.\d+)/\1/'

Tested the regex on something I'm familiar with, preg_match, and it looks solid

php > $str = 'MXN-PKG-0.3.6-62.noarch.rpm';
php > preg_match('/(\d\.\d\.\d+)/', $str, $matches);
php > var_dump($matches);
array(2) {
  [0]=>
  string(5) "0.3.6"
  [1]=>
  string(5) "0.3.6"
}

Where am I going wrong with sed? I'm using the extended regex and have looked at numerous sites showing examples of capturing the first backreference with \1.

Upvotes: 1

Views: 83

Answers (1)

anubhava
anubhava

Reputation: 785856

sed's regex engine (ERE) doesn't support \d for digits

Use this sed:

echo MXN-PKG-0.3.6-62.noarch.rpm | sed -E 's/([0-9]\.[0-9]\.[0-9]+)/[\1]/'
MXN-PKG-[0.3.6]-62.noarch.rpm

On Linux:

echo MXN-PKG-0.3.6-62.noarch.rpm | sed -r 's/([0-9]\.[0-9]\.[0-9]+)/[\1]/'
MXN-PKG-[0.3.6]-62.noarch.rpm

Upvotes: 2

Related Questions