Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Commit

Permalink
escape special characters in regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristina Munoz committed Nov 10, 2015
1 parent f153325 commit 936ab24
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions sodapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ def _perform_request(self, request_type, resource, **kwargs):

# for other request types, return most useful data
content_type = response.headers.get('content-type').strip().lower()
if re.match(r'application/json;\s*charset=utf-8', content_type):
if re.match(r'application\/json;\s*charset=utf-8', content_type):
return response.json()
elif re.match(r'text/csv;\s*charset=utf-8', content_type):
elif re.match(r'text\/csv;\s*charset=utf-8', content_type):
csv_stream = StringIO(response.text)
return [line for line in csv.reader(csv_stream)]
elif re.match(r'application/rdf+xml;\s*charset=utf-8', content_type):
elif re.match(r'application\/rdf\+xml;\s*charset=utf-8', content_type):
return response.content
else:
raise Exception("Unknown response format: {0}"
Expand Down

2 comments on commit 936ab24

@chrismetcalf
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xmunoz Do you have to escape the slashes if the regexp isn't denoted by slashes?

@xmunoz
Copy link
Owner

@xmunoz xmunoz commented on 936ab24 Nov 22, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently, no. You do need to escape + though, but the \/ escapes are actually superfluous.

>>> regex_unescaped = r'application/rdf+xml;\s*charset=utf-8'
>>> regex_escaped = r'application/rdf\+xml;\s*charset=utf-8'
>>> my_string = "application/rdf+xml; charset=utf-8"
>>> re.match(regex_unescaped, my_string)
>>> re.match(regex_escaped, my_string)
<_sre.SRE_Match object at 0x10c32db90>

Please sign in to comment.