Sample Header Ad - 728x90

Non-greedy match with SED regex (emulate perl's .*?)

50 votes
8 answers
51935 views
I want to use sed to replace anything in a string between the first AB and the *first* occurrence of AC (inclusive) with XXX. For **example**, I have this string (this string is for a test only): ssABteAstACABnnACss and I would like output similar to this: ssXXXABnnACss. ---- I did this with perl: $ echo 'ssABteAstACABnnACss' | perl -pe 's/AB.*?AC/XXX/' ssXXXABnnACss ------ but I want to implement it with sed. The following (using the Perl-compatible regex) does not work: $ echo 'ssABteAstACABnnACss' | sed -re 's/AB.*?AC/XXX/' ssXXXss
Asked by Baba (3479 rep)
Jul 22, 2016, 10:30 PM
Last activity: May 27, 2025, 08:14 PM