简体   繁体   中英

Substitute pattern multiple times in same line

I'm trying to come up with a regex search/replace to encapsulate variables (eg; $foo ) with braces (eg; ${foo} ). I came up with the following regex, but it's not working as intended:

s/"\([^"]*\)\$\([a-zA-Z0-9:]*\)\([^"]*\)"/"\1\${\2}\3"/g

However, I have some problems with this pattern. It will only do one pattern per quoted string. This is OK, since I can run the expression multiple times on the file but it doesn't handle variables already encapsulated in braces. It just puts a second set of braces on them.

My idea now is to strip \\2 of braces, but I don't think this will work since the pattern will still match and it will only perform 1 substitution per quoted string. I looked at backreference expression documentation but I'm not able to determine a better approach.

Try the following:

sed -e ':loop' -e 's/"\([^"]*\)\$\([a-zA-Z0-9:]\{1,\}\)\([^"]*\)"/"\1\${\2}\3"/' -e 't loop'

This puts the search/replace into a loop so that it will repeatedly attempt the replacement on each line until no more replacements can be done.

This works because the current line being processed is only printed when there are no more commands to be run, :loop creates a label named loop , and t loop returns execution to the label loop only if there was a successful substitution.

As mentioned by potong in comments, this can have some strange behavior on lines with multiple quotes, here is an alternative that should work properly in these scenarios:

sed -e ':loop' -e 's/^\([^"]*\("[^"]*"[^"]*\)*\)"\([^"]*\)\$\([a-zA-Z0-9:]\{1,\}\)\([^"]*\)"/\1"\3\${\4}\5"/' -e 't loop'

This might work for you (GNU sed):

echo 'a "$a" b $b "b $b b $b" $c c $c "$c c $c"' | 
sed 's/"\([^"]*\)"/"\n\1\n"/g;:a;s/\n\n//;ta;s/\n\$\([a-zA-Z0-9:]\+\)/${\1\}\n/;ta;s/\n\(.\)/\1\n/;ta'
a "${a}" b $b "b ${b} b ${b}" $c c $c "${c} c ${c}"

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