I would like to convert a command from one line to multiple:
Example Input:
myprog -library lib -topcell top -view layout
Output:
myprog \
-library lib\
-topCell top\
-view layout\
I can do that using "sed" as following:
echo $cmd | sed 's/\s-[a-zA-Z0-9]*\s/\\\n\t & /g'
But I can't replicate that by python using re.sub. What I noticed is that re.sub doesn't accept a regular expression as a second argument as following:
>>> re.sub(r'-[a-zA-Z0-9]*\s',r'[a-zA-Z0-9]*\s',cmd)
‘myprog [a-zA-Z0-9]*\\slib [a-zA-Z0-9]*\\stop [a-zA-Z0-9]*\\slayout'
Do you have a solution?
x="myprog -library lib -topcell top -view layout"
print re.sub(r"(?=-)",r"\\\n\t",x)
Try this.
In python it would be like,
>>> s = "myprog -library lib -topcell top -view layout"
>>> print re.sub(r'(-[a-zA-Z0-9]*\s)',r'\\\n\t\1', s)
myprog \
-library lib \
-topcell top \
-view layout
>>> print re.sub(r'(-[a-zA-Z0-9]*\s)',r'\\\n\t \1 ', s)
myprog \
-library lib \
-topcell top \
-view layout
In sed &
in the replacement part prints the matched characters. In python, i just used capturing groups.
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.