1

The expression is:

re.search("(%s)\\(%d)([0-9]+)".format(newspaper, year2))

where newspaper is a string and year2 is an integer.

Instead of interpreting \\ as an escape to the \ character, PyCharm interprets this as escaping ( and warns that I have an unmatched closing parenthesis in (%d). It also highlights the opening parenthesis in orange as well. When I replaced this with \\\, there is no warning but I'm not able to find my matches with this regex.

Is this how \ supposed to be escaped or am I missing another character?

as1216
  • 1,194
  • 2
  • 8
  • 11

2 Answers2

2

You need to make it a raw string.

\\ is interpreted as a single escaped slash in the string, which would escape the (, so PyCharm is correctly reporting it as an error.

So, do one of:

r"(%s)\\(%d)([0-9]+)"
# or
"(%s)\\\\(%d)([0-9]+)"

Also, you probably meant:

r"({})\\({})([0-9]+)".format(newspaper, year2)
# or
r"(%s)\\(%d)([0-9]+)" % (newspaper, year2)
Artyer
  • 31,034
  • 3
  • 47
  • 75
2

According to: https://regex101.com/

You are correct, \\ should be escaping the \

Try adding an r before your regex string and compile it, example:

regex = re.compile(r"(%s)\\(%d)([0-9]+)".format(newspaper, year2))
found = re.search(regex, stringy_thing)

You can then use the group method to probe the returned found item(s); regex101 will also show you the capture groups in the upper right pane.

Shawn
  • 561
  • 1
  • 5
  • 12