0

Simple, simple question, hope you can help me:

How do I add a string to a regex? Say:

d = '\d\d\d'
mo = re.compile(r #d goes here)

Pasting it, separating it with a comma, or with a plus gives me errors. Normally, as you know, it would be re.compile(r'\d\d\d')

falsetru
  • 357,413
  • 63
  • 732
  • 636
Mauser
  • 31
  • 5
  • 4
    The `r` is just there to prevent certain escape sequences − you don't *need* it and can just type `re.compile(r)` or (perhaps better), `d = r'\d\d\d'` `re.compile(d)` ... Also [see this question](http://stackoverflow.com/q/2081640/660921). – Martin Tournoij Mar 24 '16 at 06:17

2 Answers2

0

Is this what you are looking for?

d = r"\d\d\d"
re.compile(d)
RootTwo
  • 4,288
  • 1
  • 11
  • 15
0

Maybe more intuitive:

d = r"\d{3}"
# match a digit exactly three times, consecutively
re.compile(d)
Jan
  • 42,290
  • 8
  • 54
  • 79