0

I have a string in python as below:

"\\B1\\B1xxA1xxMdl1zzInoAEROzzMofIN"

I want to get the string as

"B1xxA1xxMdl1zzInoAEROzzMofIN"

I think this can be done using regex but could not achieve it yet. Please give me an idea.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
gishara
  • 815
  • 2
  • 15
  • 41

3 Answers3

3
st = "\B1\B1xxA1xxMdl1zzInoAEROzzMofIN"
s = re.sub(r"\\","",st)
idx = s.rindex("B1")
print s[idx:]

output = 'B1xxA1xxMdl1zzInoAEROzzMofIN'

OR

st = "\B1\B1xxA1xxMdl1zzInoAEROzzMofIN"
idx = st.rindex("\\")
print st[idx+1:]

output = 'B1xxA1xxMdl1zzInoAEROzzMofIN'

RanRag
  • 48,359
  • 38
  • 114
  • 167
  • These look a bit too specific for me. What if the source is `"123\\456\\789"` - neither solution will give `"123789"` as a result. – Tim Pietzcker Jan 06 '12 at 08:15
  • his example was specific too, so i came with this specific solution. I thought he was facing problem with this specific string. – RanRag Jan 06 '12 at 08:22
  • Thanks a lot RanRag. The second solution what I needed. – gishara Jan 06 '12 at 09:59
  • 1
    Note that this will work if you have **only one** double backslash. Yet, for this case, using list slicing is simple and efficient. – JMax Jan 06 '12 at 13:27
3

Here is a try:

import re
s = "\\B1\\B1xxA1xxMdl1zzInoAEROzzMofIN"
s = re.sub(r"\\[^\\]+\\","", s)
print s

Tested on http://py-ide-online.appspot.com (couldn't find a way to share though)

[EDIT] For some explanation, have a look at the Python regex documentation page and the first comment of this SO question:

How to remove symbols from a string with Python?

because using brackets [] can be tricky (IMHO)

In this case, [^\\] means anything but two backslashes \\.

So [^\\]+ means one or more character that matches anything but two backslashes \\.

Community
  • 1
  • 1
JMax
  • 26,109
  • 12
  • 69
  • 88
  • You make `\w` optional, and they may not be word chars, why not replace `\w*` with `[^\\]+`? You'd then have a general pattern – fge Jan 06 '12 at 08:40
  • @fge: thanks for the suggestion, I've edited my answer with your code (it took me some time to check this as I am not that familiar to Python Regex) – JMax Jan 06 '12 at 08:50
0

If the desired section of the string is always on the RHS of a \ char then you could use:

string = "\\B1\\B1xxA1xxMdl1zzInoAEROzzMofIN"
string.rpartition("\\")[2]

output = 'B1xxA1xxMdl1zzInoAEROzzMofIN'

Alex L
  • 8,748
  • 5
  • 49
  • 75