1

I've been trying several things to use variables inside a regular expression. None seem to be capable of doing what I need.

I want to search for a consecutively repeated substring (e.g. foofoofoo) within a string (e.g. "barbarfoofoofoobarbarbar"). However, I need both the repeating substring (foo) and the number of repetitions (In this case, 3) to be dynamic, contained within variables. Since the regular expression for repeating is re{n}, those curly braces conflict with the variables I put inside the string, since they also need curly braces around.

The code should match foofoofoo, but NOT foo or foofoo.

I suspect I need to use string interpolation of some sort.

I tried stuff like

n = 3
str = "foo"
string = "barbarfoofoofoobarbarbar"
match = re.match(fr"{str}{n}", string)

or

match = re.match(fr"{str}{{n}}", string)

or escaping with

match = re.match(fr"re.escape({str}){n}", string)

but none of that seems to work. Any thoughts? It's really important both pieces of information are dynamic, and it matches only consecutive stuff. Perhaps I could use findall or finditer? No idea how to proceed.

Something I havent tried at all is not using regular expressions, but something like

if (str*n) in string:
    match

I don't know if that would work, but if I ever need the extra functionality of regex, I'd like to be able to use it.

  • Does [this](https://stackoverflow.com/questions/6930982/how-to-use-a-variable-inside-a-regular-expression) answer your question? – chash May 13 '20 at 22:32
  • 1
    `match()` only matches at the beginning of a string. So even once you get the escaping figured out, you still won't get a match with `(foo){3}` This is ugly but matches: `match = re.search(fr"({s}){{{n}}}", string)` (use double braces for literal brace). – Mark May 13 '20 at 22:34
  • Sadly, it does not, as in that case he didn't have to use curly braces in the regex. I checked for other questions, none works. – Lui Martinez Laskowski May 13 '20 at 22:35
  • 1
    There's literally a section called "Curly Braces". Maybe I'm missing something. – chash May 13 '20 at 22:35
  • Mark, or anyone, would you explain why do you need 3 nested curly braces for n? Also, are those parentheses strictly necessary in a re{n} expression? – Lui Martinez Laskowski May 13 '20 at 22:38
  • 1
    @LuiMartinezLaskowski you need braces for the f-string to interpolate your variable, but you also need double braces to get the literal brace characters into the string for the regex. You could just look at the string created by the variations outside of the regex context. – Mark May 13 '20 at 22:39

1 Answers1

2

For the string barbarfoofoofoobarbarbar, if you wanted to capture foofoofoo, the regex would be r"(foo){3}". if you wanted to do this dynamically, you could do fr"({your_string}){{{your_number}}}".

If you want a curly brace in an f-string, you use {{ or }} and it'll be printed literally as { or }.

Also, str is not a good variable name because str is a class (the string class).