Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X 'RE error: invalid repetition count(s)'

Tags:

regex

macos

sed

I'm trying to replace the following (in it's simplest form)

{filedir_9}file.jpg

with

{filedir_7}file.jpg

Using

sed -i -e 's/(\{filedir_9\})([a-z\-\_0-9]+).jpg/\{filedir_7\}$2$3/g'

But I'm getting : RE error: invalid repetition count(s)

like image 951
wot Avatar asked Oct 24 '25 19:10

wot


1 Answers

You may use

sed -i '' -e 's/{filedir_9}\([-a-z_0-9]\{2,\}\)\.jpg/{filedir_7}\1/g'

Note that \{ opens a limiting quantifier in a POSIX BRE pattern, you need { to match a literal {.

To create a capturing group in a BRE POSIX pattern, you need \(...\), not (...) and inside the replacement, you should use \1 to refer to Group 1 value.

In POSIX patterns, escaped sequences inside bracket expressions are not supported, you should put - at the start/end of the pattern, escaping it does not work (the \ is treated as a literal \ char).

Also, to match a dot, you need to escape the . char in the pattern, if it is unescaped, it matches any char.

Inside the replacement string, you should use \1 rather than $1 (Perl-like placeholders). Note you are using placeholders to Group 2 and 3, while your (\{filedir_9\})([a-z\-\_0-9]+).jpg pattern only attempts to capture 2 substrings, and thus there is no Group 3 (so, no point using $3 or \3, it is empty).

like image 77
Wiktor Stribiżew Avatar answered Oct 28 '25 00:10

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!