-1

I am attempting to detect the following pattern: even number of \ followed by $ and text.

This is valid: \\$hello or $goodbye.

I am trying to do this in Python:

txt = r"\\$hello"
regex = r"(?<!\\)(\\\\)*(?!\\)\$[a-zA-Z_]\w*"

x = re.findall(regex, txt)

if x:
  print(x)
else:
  print("No match")

When I run this, I get this output ['\\\\'] even though when I try it here: https://regex101.com/, I get a full match of \\$hello. How can I adjust this to get the entire portion to get matched? Or even better, just the part without the slashes?

Other things I've tried:

  1. Remote escaping characters in regex: r"(?<!\)(\\\)*(?!\)\$[a-zA-Z_]\w*" This leads to error re.error: missing ), unterminated subpattern at position 11
shurup
  • 751
  • 10
  • 33
  • @jonathan.scholbach I am pretty sure that in raw expressions, \ doesn't need to be escaped. My text input is correct. It can be written like this: `"\\\\$hello"` or `r"\\$hello"`. Both means the same thing (two slashes followed by $hello). – shurup Oct 19 '20 at 21:27

1 Answers1

-1

I don't understand the idea behind your regex, so I cannot really say where you went wrong. But the following works with your verbal description of the matching pattern ("an even number of backslashes, followed by a dollar sign, followed by text") and retrieves the text after the $ sign:

import re


txt = r"\\$hello"
regex = r"(\\)*\$(.*)"
match = re.findall(regex,txt)[0][1] 

If you want the dollar sign included in the matchstring, just adapt:

regex = r"(\\)*(\$.*)"
Jonathan Scholbach
  • 4,925
  • 3
  • 23
  • 44