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:
- Remote escaping characters in regex:
r"(?<!\)(\\\)*(?!\)\$[a-zA-Z_]\w*"
This leads to errorre.error: missing ), unterminated subpattern at position 11