1

In the below example I want to only return True for the last string in the Series. This means I have to add a conditional escaping any plus symbols so that they actually get searched and it doesn't take on its special regex function.

import pandas as pd
testseries=pd.Series(["product a basic","product a","product a+","product b basic","product b","product b+"])

#this string will be unknown in practice
searchstring="b+"

# Necessary cleaning conditional
if "+" in searchstring:
    searchstring = searchstring.replace("+","\\+")

#Search is actually performed
print(searchstring)
testseries.str.contains(searchstring)

In practice I am not going to know what searchstring is going to be. Is there a function or different approach for ensuring that any characters that need to be escaped are correctly escaped?

I don't want to keep having to create a ever increasing chain of conditionals that clean the searchstring or is this inevitable?

DataJack
  • 341
  • 2
  • 13

1 Answers1

2

The python re module has an escape() function. Here is an example how to use it:

>>> import re 
>>> re.escape('b+')
'b\\+'
smaftoul
  • 2,375
  • 17
  • 14
  • 1
    Thanks for the quick response, my googling was failing me. Now I know what to search my question is possibly a duplicate of https://stackoverflow.com/questions/280435/escaping-regex-string-in-python?rq=1 – DataJack Feb 18 '20 at 16:25