-2
import re
response = 'string contains+ as special character'
re.match(response, response)
print match

The string match is not successful as the strring contains the special character '+' . If any other special character , then match is successfull. Even if putting back slash in special character , it doesnt match. Both doesnt match:

response = r'string contains\+ as special character'



response = 'string contains\\+ as special character'

How to match it when the string is a variable and has this special character.

  • 2
    I am sure I saw that post before :D Why do you match a string against itself? – DYZ Jul 09 '20 at 07:44
  • `re.match(r'string contains\+ as special character', response)` – yatu Jul 09 '20 at 07:45
  • 1
    Please change the shown code to avoid the impression that the regexes you are discussing are not the ones used. With the given code, neither of them is applied. Instead the string is used as regex to match itself. Please show code which uses different strings for response and regex. – Yunnosch Jul 09 '20 at 07:45

2 Answers2

0

If you want use an arbitrary string and in a regex but treat it as plain text (so the special regex characters don't take effect), you can escape the whole string with re.escape.

>>> import re
>>> response = 'string contains+ as special character'
>>> re.match(re.escape(response), response)
<re.Match object; span=(0, 37), match='string contains+ as special character'>
khelwood
  • 55,782
  • 14
  • 81
  • 108
0

In the general case, an arbitrary string does not match itself, though of course this is true for any string which doesn't contain regex metacharacters.

There are several characters which are regex metacharacters and which do not match themselves. A corner case is . which matches any character (except newline, by default), and so of course it also matches a literal ., but not exclusively. The quantifiers *, +, and ? as well as the generalized repetition operator {m,n} modify the preceding regular expression, round parentheses are reserved for grouping, | for alternation, square brackets define character classes, and finally of course the backslash \ is used to escape any of the preceding metacharacters, or itself.

Depending on what you want to accomplish, you can convert a string to a regex which matches exactly that literal string with re.escape(); but perhaps you simply need to have an incorrect assumption corrected.

tripleee
  • 175,061
  • 34
  • 275
  • 318