a = "\\Virtual Disks\\DG2_ASM04\\ACTIVE"
From the above string I would like to get the part "DG2_ASM04"
alone. I cannot split or strip as it has the special characters "\\"
, "\\D"
and "\\A"
in it.
Have tried the below and can't get the desired output.
a.lstrip("\Virtual Disks\\").rstrip("\ACTIVE")
the output I have got is: 'G2_ASM04' instead of "DG2_ASM04"
Simply use slicing
and escape
backslash( \\
)
>>> a.split("\\")[-2]
'DG2_ASM04'
In your case D
is also removing because it is occurring more than one time in given string (thus striping D
as well). If you tweak your string then you will realize what is happening
>>> a = "\Virtual Disks\XG2_ASM04\ACTIVE"
>>> a.lstrip('\\Virtual Disks\\').rstrip("\\ACTIVE")
'XG2_ASM04'
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.