简体   繁体   中英

How to convert \1 character as a string in python

I am trying to form the command as bellow using python. python is not considering \\1 as a string, seems it has a special meaning. what escape character should I use to make python consider \\1 as string?


Python variable: 
cmd="oc get pod |egrep -v Evicted| grep -Pv '\s+([1-9]+)\/\1\s+'"


Getting actual output: 
oc get pod |egrep -v Evicted|  grep -Pv '\s+([1-9]+)\/\s+'

Expected string output: 
 
oc get pod |egrep -v Evicted |grep -Pv '\s+([1-9]+)\/\1\s+'

missing \1 in the last grep 

You can either double-escape your backslashes

"\\1"

or you can use a raw string literal

r"\1"

Note that if you do the latter, then you can't use the usual escape sequences either, so \\n in a raw string literal is literally a backslash followed by the letter 'n', not a newline.

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