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

  • SEARCH
  • Home
  • 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 8912087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:06:47+00:00 2026-06-15T04:06:47+00:00

I have a wikipedia dump and struggling with finding appropriate regex patter to remove

  • 0

I have a wikipedia dump and struggling with finding appropriate regex patter to remove the double square brackets in the expression. Here is the example of the expressions:

line = 'is the combination of the code names for Herbicide Orange (HO) and Agent LNX, one of the [[herbicide]]s and [[defoliant]]s used by the [[United States armed forces|U.S. military]] as part of its [[herbicidal warfare]] program, [[Operation Ranch Hand]], during the [[Vietnam War]] from 1961 to 1971.'

I am looking to remove all of the square brackets with the following conditions:

  • if there is no vertical separator within square bracket, remove the brackets.

    Example : [[herbicide]]s becomes herbicides.

  • if there is a vertical separator within the bracket, remove the bracket and only use the phrase after the separator.

    Example : [[United States armed forces|U.S. military]] becomes U.S. military.

I tried using re.match and re.search but was not able to arrive to the desired output.

Thank you for your help!

  • 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-06-15T04:06:48+00:00Added an answer on June 15, 2026 at 4:06 am

    What you need is re.sub. Note that both square brackets and pipes are meta-characters so they need to be escaped.

    re.sub(r'\[\[(?:[^\]|]*\|)?([^\]|]*)\]\]', r'\1', line)
    

    The \1 in the replacement string refers to what was matched inside the parentheses, that do not start with ?: (i.e. in any case the text you want to have).

    There are two caveats. This allows for only a single pipe between the opening and closing brackets. If there are more than one you would need to specify whether you want everything after the first or everything after the last one. The other caveat is that single ] between opening and closing brackets are not allowed. If that is a problem, there would still be a regex solution but it would be considerably more complicated.

    For a full explanation of the pattern:

    \[\[        # match two literal [
    (?:         # start optional non-capturing subpattern for pre-| text
       [^\]|]   # this looks a bit confusing but it is a negated character class
                # allowing any character except for ] and |
       *        # zero or more of those
       \|       # a literal |
    )?          # end of subpattern; make it optional
    (           # start of capturing group 1 - the text you want to keep
        [^\]|]* # the same character class as above
    )           # end of capturing group
    \]\]        # match two literal ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have downloaded Wikipedia latest dump and parsed it a MySQL Database. Now i
I have this XML file of size 2.8GB (Polish Wikipedia dump). I have to
well,the topic introduce Isolation in wikipedia have such words,links is here ......... Serializable This
here are some question after I have looked at wikipedia: Association: Association represents the
I am looking for the XSD schema of RSS. Via Wikipedia I have found
I have the following JSON string (from wikipedia http://en.wikipedia.org/wiki/JSON ) { name:Product, properties: {
I have read about LDAP on wikipedia and I kind of understand what it
i have tried to copy euro symbol from Wikipedia...and echo it (in my parent
I have read the explanation in http://en.wikipedia.org/wiki/PageRank and i understand that the page rank
I have a website I want to regexp on, say http://www.ru.wikipedia.org/wiki/perl . The site

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.