If you want to check something around the template, you can use the lookahead and lookbehind statements. They are not part of the match, but they say that you expect to find (or not find) around it.
Since we do not need parentheses, we need to use a negative lookbehind and lookahead.
A negative lookahead looks like (?!...)
, where it matches if ...
doesn't go any further. Similarly, a negative lookbehind looks like (?<!...)
and matches if ...
doesn't come earlier.
Our example is a bit more complicated because we use [
and ]
, which themselves make sense in regular expressions, so we need to avoid them with \
.
So, we can create a template as follows:
- Negative lookbehind for
[
- (?<!\[)
- Four digits -
\d{4}
- Negative result for
]
- (?!\])
This gives us the following Python code:
>>> import re >>> r = re.compile("(?<!\[)\d{4}(?!\])") >>> r.match(" 2011 ") >>> r.search(" 2011 ") <_sre.SRE_Match object at 0x10884de00> >>> r.search("[2011]")
To rename, you can use the re.sub
function or the sub
function on the compiled template. To make it work, you need to add an extra set of brackets throughout the year to mark it as a group.
Also, when specifying your replacement, you refer to the group as \1
, so you need to avoid \
or use an unprocessed string.
>>> r = re.compile("(?<!\[)(\d{4})(?!\])") >>> name = "2011 - This Year" >>> r.sub(r"[\1]",name) '[2011] - This Year'
source share