The new fstrings in Python interpret brackets in their own way. You can escape brackets you want to see in the output by doubling them:

regex = fr'(\d{{1,2}})/(\d{{1,2}})/(\d{{4}}|\d{{2}})'
Answer from Blckknght on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to use f-string inside regex pattern?
r/learnpython on Reddit: How to use f-string inside Regex Pattern?
March 22, 2022 -

Hello all, this be my code:

zipCode = "AA 45522"
myPat1 = "(\d{1,9})([^,]+?)AA 45522" # the pattern i want

# now interpolating variable name into the regex pattern
myPat2 = f"(\d{1,9})([^,]+?){zipCode}" 
myPat3 = rf"(\d{1,9})([^,]+?){zipCode}" #trying raw 
myPat4 = "(\d{1,9})([^,]+?)"+zipCode

# Results I see in debugger:

myPat1 = '(\\d{1,9})([^,]+?)AA 45522' # correct
myPat4 = '(\\d{1,9})([^,]+?)AA 45522' # correct 
myPat2 = '(\\d(1, 9))([^,]+?)AA 45522' # faulty
myPat3 = '(\\d(1, 9))([^,]+?)AA 45522' # faulty

myPat2 & myPat3 are adding a space between (1, & 9) which is resulting into creation of faulty pattern (which in turn fails to find pattern in my rawString). So why does using f-string inside regex pattern fudge things up? I recall facing same problem few weeks back.

Finally I have to use myPat4 style of coding to make it work, but I love f-strings and want to find a proper solution to using f-strings inside regex pattern. Thanks.

๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Regular expressions and f-strings - Python Help - Discussions on Python.org
September 17, 2024 - I am trying to use f-strings to construct regular expressions. Unfortunately, regular expressions make heavy use of the { and } symbols and f-strings do not support escaping them with \. An example is shown below: re = rf'\{,2\}โ€™ I was hoping to see the string โ€˜{,2}โ€™ in re but instead, I got this Python error: SyntaxError: f-string expression part cannot include a backslash.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to use f-strings with regex and use brackets to specify "instances of"
r/learnpython on Reddit: How to use f-strings with regex AND use brackets to specify "instances of"
May 11, 2021 -

[SOLVED]

Hello everyone,

Very simple question, that I couldn't find the answer of.

Basically, I want to use an f-string in my regex, but brackets are used to define a specific number of instances. I.E.

>>> import re
>>> word='AAA'
>>> re.search('[A-Z]{3}',word)
<re.Match object; span=(0, 3), match='AAA'>
>>> pre_defined_variable='A'
>>> re.search(f'{pre_defined_variable}{3}',word)
>>> re.search(f'{pre_defined_variable}',word)
<re.Match object; span=(0, 1), match='A'>

So you can see above, using f-strings does work, but only if you are not using the regex brackets for "how many instances of". How do I use both?

๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Using {} regex within re.sub when using f-string - Python Help
March 20, 2024 - Hi, I run into an issue when I was trying to use regex with f-string in re.sub Please see the below code exhibits. When I analyzed more I assumed that when I am using f-string the interpreter tries to resolve {128} as a variable. But I want it to be seen as a regex [0-9,a-f]{128} How could we achieve that without changing the pattern.
๐ŸŒ
death and gravity
death.andgravity.com โ€บ f-re
The unreasonable effectiveness of fโ€-โ€strings and re.VERBOSE - death and gravity
Formatted string literals (fโ€-โ€strings) were added in Python 3.61, and provide a way to embed expressions inside string literals, using a syntax similar to that of str.format():
๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-put-variable-in-string
PyTutorial | How to put variable in regex pattern in Python
January 10, 2023 - We'll see how to put a variable in regex using f-string in the following example: import re txt = "Hello Python" # My variable var = "Python" # Regex with f-string r = re.findall(f"\s{var}", txt) # Result print(r)
Find elsewhere
๐ŸŒ
Vyeron
tutorials.vyeron.com โ€บ regex-tutorial-python-f-string-regex
Regex Tutorial | Python F-string regex - Tutorials.Vyeron.com
July 21, 2024 - Pythonโ€™s F-strings provide a concise and readable way to embed expressions inside string literals, combining F-strings with regular expressions (regex) can be a powerful tool for string manipulation and pattern matching.
๐ŸŒ
Medium
medium.com โ€บ @suryasekhar โ€บ string-prefixes-in-python-what-are-f-strings-and-r-strings-in-python-ca759810ebfa
String Prefixes in Python: What are f-strings and r-strings in Python | by Surya Sekhar Datta | Medium
September 15, 2024 - Discover how Pythonโ€™s f-strings simplify string interpolation and how to use r-strings to handle raw text like file paths and regex.
๐ŸŒ
W3docs
w3docs.com โ€บ home โ€บ learn python โ€บ f-strings
Python f-Strings โ€” Complete Guide with Examples | W3docs
An f-string is any string literal that starts with the letter f (or F) immediately before the opening quote. Any Python expression you place inside curly braces {} is evaluated at runtime and its value is converted to a string and inserted into ...
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ prefix a string with `rf`to get raw f-strings
r/Python on Reddit: prefix a string with `rf`to get raw f-strings
April 22, 2021 -

today, while answering a question on stackoverflow, another user suggested I use a raw string when generating a regex pattern dynamically. I was using f-strings to build the regex like this.

words = ['foo', 'bar', 'baz']
pat = f"\\b({ '|'.join(words)})\\b"
# pat -> '\\b(foo|bar|baz)\\b'

but following the kind user's comment, I tried out the following and it works!

words = ['foo', 'bar', 'baz']
pat = rf"\b({'|'.join(words)})\b"
# pat -> '\\b(foo|bar|baz)\\b'

TIL something new & something I'll probably be using in many places.

๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
F-strings in match case - Ideas - Discussions on Python.org
June 30, 2023 - Allow f-strings to be evaluated in a match case setting match Foo: case f"Greater than {Bar}": print("Foo > Bar") This currently raises a SyntaxError: patterns may only match literals and attribute lookups
๐ŸŒ
Real Python
realpython.com โ€บ python-f-strings
Python's F-String for String Interpolation and Formatting โ€“ Real Python
November 30, 2024 - Python's f-strings provide a readable way to interpolate and format strings. They're readable, concise, and less prone to error than traditional string interpolation and formatting tools, such as the .format() method and the modulo operator ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ fstring
Python f-string
A Python f-string (formatted string literal) allows you to insert variables or expressions directly into a string by placing them inside curly braces {}. In this tutorial, you will learn about the Pyt
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ regex.html
Regular expression HOWTO โ€” Python 3.14.6 documentation
Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module. Using this little language, you specify the rules for the set of possible strings that you want to ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ formatted-string-literals-f-strings-python
f-strings in Python - GeeksforGeeks
June 19, 2024 - To create an f-string, prefix the string with the letter โ€œ f โ€. The string itself can be formatted in much the same way that you would with str.format(). F-strings provide a concise and convenient way to embed Python expressions inside string ...
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
How to Use f-strings in Python | note.nkmk.me
May 18, 2023 - Python 3.6 introduced a new feature, known as formatted string literals, or f-strings, to simplify the use of the string method format(). Lexical analysis - Formatted string literals โ€” Python 3.11.3 ...