Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6850813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:10:16+00:00 2026-05-27T01:10:16+00:00

Python metacharacter negation. After scouring the net and writing a few different syntaxes I’m

  • 0

Python metacharacter negation.

After scouring the net and writing a few different syntaxes I’m out of ideas.

Trying to rename some files. They have a year in the title e.g. [2002].
Some don’t have the brackets, which I want to rectify.

So I’m trying to find a regex (that I can compile preferably) that in my mind looks something like (^[\d4^]) because I want the set of 4 numbers that don’t have square brackets around them. I’m using the brackets in the hope of binding this so that I can then rename using something like [\1].

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:10:16+00:00Added an answer on May 27, 2026 at 1:10 am

    If you want to check for things around a pattern you can use lookahead and lookbehind assertions. These don’t form part of the match but say what you expect to find (or not find) around it.

    As we don’t want brackets we’ll need use a negative lookbehind and lookahead.

    A negative lookahead looks like this (?!...) where it matches if ... does not come next. Similarly a negative lookbehind looks like this (?<!...) and matches if ... does not come before.

    Our example is make slightly more complicated because we’re using [ and ] which themselves have meaning in regular expressions so we have to escape them with \.

    So we can build up a pattern as follows:

    • A negative lookbehind for [ – (?<!\[)
    • Four digits – \d{4}
    • A negative lookahead 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 your compiled pattern. To make it work you’ll need to add an extra set of brackets around the year to mark it as a group.

    Also, when specifying your replacement you refer to the group as \1 and so you have to escape the \ or use a raw string.

    >>> r = re.compile("(?<!\[)(\d{4})(?!\])")
    >>> name = "2011 - This Year"
    >>> r.sub(r"[\1]",name)
    '[2011] - This Year'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python's convention is that variables are created by first assignment, and trying to read
Python newb here. I m trying to count the number of letter as in
Python documentation says that os.rename(src, dst) ... On Windows, if dst already exists, OSError
python newbie here. I'm writing the code to control an experiment that has multiple
Python newbie here. I'm trying to create a black jack game in which the
Python newbie here. I'm trying to package a console app following this doc .
Python 2.7.1: print -34 % 4 # outputs 2 Java 1.5.0: System.out.println(-34 % 4);
Python newb...beware! I am trying to recursively ftp some files. In the script below,
Python newbie here. I'm writing a script that can dump some output to either
Python newbie here: I'm writing a market simulation in Python using Pysage, and want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.