Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I'm using the regex library in python and trying to do fuzzy matching.

I need to use a variable in my search string:

x = regex.search(rf"(?b){variable}{d}",s)

which gives 

NameError: name 'd' is not defined

The issue is that I want to specify the type of error with the {d} tag but that is also interpreted as a variable. The only solution I have come up with so far is just to assign d = "d". Looking for a more "proper" way to accomplish this.

Example case:

variable = "amazing"

s = 'amaing analogy'

x = regex.search(rf"{variable}{d}",s)

x.group()

Intended output is: "amazing"

1 Answer

0 votes
by (36.8k points)

You can escape currently brackets using the adding another currently brackets

x = regex.search(rf"(?b){variable}{{d}}",s)

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch 

Browse Categories

...