Try r'\(#\d+\.\d+\)\}'
The (
, )
, .
, and }
are all special metacharacters, that's why they're preceded by \
, so they're matched literally instead.
You also need to apply the +
repetition at the right element. Here it's attached to the \d
-- the shorthand for digit character class -- to mean that only the digits can appear one-or-more times.
The use of r'raw string literals'
makes it easier to work with regex patterns because you don't have to escape backslashes excessively.
See also
Variations
For instructional purposes, let's consider a few variations. This will show a few basic features of regex. Let's first consider one of the attempted patterns:
\(\#+(\d\.\d)+\)\}
Let's space out the parts for readability:
\( \#+ ( \d \. \d )+ \) \}
\__________/
this is one group, repeated with +
So this pattern matches:
- A literal
(
, followed by one-or-more #
- Followed by one-or-more of:
- A digit, a literal dot, and a digit
- Followed by a literal
)}
Thus, the pattern will match e.g. (###1.23.45.6)}
(as seen on rubular.com). Obviously this is not the pattern we want.
Now let's try to modify the solution pattern and say that perhaps we also want to allow just a sequence of digits, without the subsequent period and following digits. We can do this by grouping that part (…)
, and making it optional with ?
.
BEFORE
\(#\d+\.\d+\)\}
\___/
let's make this optional! (…)?
AFTER
\(#\d+(\.\d+)?\)\}
Now the pattern matches e.g. (#1.23)}
as well as e.g. (#666)}
(as seen on rubular.com).
References