0

I am having a hard time in matching the string "\" with a regualar expression. I tried the following but it did not work.

print re.sub('([\"\\\\\"])', "-", myText, 0)

Any idea?

Thanks,

Stefano
  • 3,981
  • 8
  • 36
  • 66
  • 1
    You know you can just do: `myText.replace("\\", "-")`. Regex seems kinda overkill here. –  May 19 '14 at 16:22
  • You've specified a [character class](http://www.regular-expressions.info/charclass.html) -- `[...]`. It will only match a single character, in this case either a quote or a backslash. If you want to match quotes and slashes in a particular order, then drop the brackets. – Jonathan Lonowski May 19 '14 at 16:25
  • 1
    In general, regex patterns should always be represented with raw strings to avoid the problem you experienced. Refer to 3rd paragraph of https://docs.python.org/2/library/re.html – IceArdor May 19 '14 at 16:31
  • The string i meant included the quotes... so i want to get quote + backslash + quote – Stefano May 20 '14 at 05:54

3 Answers3

5

@iCodez is right, but if you really want to use regex:

>>> re.sub(r"\\", "-", r"this\and\that")
'this-and-that'

Note the use of r to specify a raw string.

EDIT: Actually, re-reading your question it's not entirely clear whether you want to replace \ or "\" - in the latter case, you'll want:

>>> re.sub(r'"\\"', "-", r'This "\" string "\" is "\" odd.')
'This - string - is - odd.'

... which, again as iCodes points out, is simpler with a straight replace():

>>> text = r'This "\" string "\" is "\" odd.'
>>> text.replace(r'"\"', '-')
'This - string - is - odd.'
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • Thanks for the help... just a curiosity... what does the little r do just before a string? – Stefano May 20 '14 at 06:01
  • @Stefano see [What exactly do “u” and “r” string flags do in Python, and what are raw string literals?](http://stackoverflow.com/q/2081640/1014938) – Zero Piraeus May 20 '14 at 12:52
1

Backslash serves as an escape character. For every single (\) backslash you need two backslashes (\\).

The use of r is Python’s raw string notation for regular expression patterns and to avoid escaping.

>>> re.sub(r'"\\"', '-', r'foo "\" bar "\" baz')
'foo - bar - baz'

If you were just wanting to replace the backslash itself without the quotes around it.

>>> re.sub(r'\\', '-', r'foo\bar\baz')
'foo-bar-baz'
hwnd
  • 69,796
  • 4
  • 95
  • 132
0

I think your regex needs some tweaking. If I understand what you are trying to do, you want to replace "\" with -.

 print re.sub('\"\\\\\"', "-", myText, 0)
woot
  • 7,406
  • 2
  • 36
  • 55
  • Please try to flesh out your answer a bit more. Just posting a code block without any explanation or context is not good. In its current state your answer might not be of any use to people looking for an answer to this question in the future. Also your answer does not contain any information that is not already contained by any of the other answers and without being an expert in Python I doubt that your answer is what the OP is looking for, see the other answers... – Xaver Kapeller May 19 '14 at 16:53
  • To me it was rather obvious that I'm only fixing his regex. Further regarding my answer already being in other answers... I thought I answered first. – woot May 19 '14 at 17:02