0

Is this the correct way to remove everything between two backward slashes?

clean = re.sub(r'\\.+?\\', '', clean)

Example input:

a\ue00f\ue010\ue011\ue012\ue013\a

Example output:

aa
8
  • 2
    These are backslashes not forward slashes. Commented Feb 1, 2015 at 21:03
  • 4
    Are the things like \ue00f literally that or are they Unicode characters? Commented Feb 1, 2015 at 21:04
  • 1
    I would assume literally, how else could they be replaced? Commented Feb 1, 2015 at 21:04
  • 1
    Well, does it work? (My guess: No, because it is non-greedy, because of your ? after the plus. Remove that and it'll be greedy.) Commented Feb 1, 2015 at 21:12
  • 2
    @MalikBrahimi: I don't think that that's a fair assumption. The OP is probably looking at repr() output and not understanding that the \uhhhh escape sequences are really single Unicode codepoints that are not printable. That needs to be clarified before the question is answerable. Commented Feb 1, 2015 at 21:16

1 Answer 1

1

Maybe split() can help here:

>>> input = r'a\ue00f\ue010\ue011\ue012\ue013\a'
>>> elems = input.split('\\')
>>> ''.join((elems[0], elems[-1]))
'aa'
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't there a way to just match anything between backward slashes and just replace it with nothing?
@Sam I'm not sure why there would be such a thing built into the language... but that's essentially what I'm doing. What's not helpful about the solution I've offered?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.