0

I typed the following line in Python shell:

In [67]: print(re.match(r"\d", "\\ddfss")) 

None

Since "\d" is a raw string, I expect that re.match will return a match object for the string "\ddfss". Do you know why it doesn't happen?

CrazySynthax
  • 9,442
  • 21
  • 70
  • 136
  • `\d` matches a *digit*. [To match ``\``, you need to double the ``\`` - ``r'\\d'``.](https://ideone.com/4LoeLh) – Wiktor Stribiżew May 19 '16 at 08:55
  • And here is a more proper original: [*Python how to replace backslash with re.sub()*](http://stackoverflow.com/questions/10585349/python-how-to-replace-backslash-with-re-sub) – Wiktor Stribiżew May 19 '16 at 09:00
  • @WiktorStribiżew, r"" mean string literals, meaning that the expression actually means a character of '\' and a character of 'n' – CrazySynthax May 19 '16 at 10:02
  • @CrazySyntax: Exactly, a literal ``\`` and a literal `n`. And a literal `\d` is a digit regex pattern. When you define regex patterns, use raw string literals - it is a common advice. – Wiktor Stribiżew May 19 '16 at 10:08
  • @WiktorStribiżew . OK... So, how can you explain this exaple? In [106]: print(re.match(r"\d", "o6fss")) None "\d" is supposed to look for a digit. You have a digit in the string "o6fss", and it still finds no match. – CrazySynthax May 19 '16 at 10:12
  • 1
    You have asked another dupe, see the link there. You miss up on understanding the difference between `re.match` and `re.search`. Too many identical questions have been asked, and keep on being asked :( – Wiktor Stribiżew May 19 '16 at 10:14

0 Answers0