Python regular expression negates metacharacters

Negation of a metacharacter in Python.

After cleaning the net and writing a few different syntaxes, I have no idea.

Trying to rename some files. They have a year in the name, for example. [2002]. Some do not have the brackets that I want to fix.

So, I'm trying to find a regular expression (which I can compile) that in my mind looks something like (^[\d4^]) , because I want the set of four numbers to not have square brackets around them. I use parentheses in the hope of tying this together to later rename using something like [\1] .

+4
source share
1 answer

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' 
+9
source

Source: https://habr.com/ru/post/1383012/


All Articles