简体   繁体   中英

Regular expression substitution with whitespace (perl)

I would like to replace all matches of the following perl regular experssion

s@typedef\s*([\w\s<>,:]*)\s(\w+)\s*;@using \2= \1;@g

in a file.

I have done this with :

/usr/bin/perl -p -i -e "s@typedef\s*([\w\s<>,:]*)\s(\w+)\s*;@using \2= \1;@g"  ./file.txt

The problem, the substituion does not work as long as I leave the whitespaces in "using \\2= \\1" ? how can I make this work?

ERROR: Substitution replacement not terminated at -e line 1.

The shell in which you're invoking your perl command is doing substitution on the regular expression that you're passing in.

Use

/usr/bin/perl -p -i -e 's@typedef\s*([\w\s<>,:]*)\s(\w+)\s*;@using \2= \1;@g'  ./file.txt

instead.

The difference here is that the regex is in single quotes. This prevents shell substitution.

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