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 8023931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:42:37+00:00 2026-06-04T22:42:37+00:00

I need a regex in python that matches any char that is surrounded by

  • 0

I need a regex in python that matches any char that is surrounded by exactly 2 underscores.
meaning, meaning

__a__ 

will match “a”,
but

___a___ 

will not match.
it needs to support overlapping matches,
such that

__a__d___b___e__c__ 

will return “ac”
because a is surrounded by double underlines, but d,e have a triple one next to them and b has a triple underline on both sides.
what I have now

(?<=[_]{2})(.)(?=[_]{2})

it solves the overlapping, but not the “exactly 2”
in the above example it returns “adbec”

  • 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-04T22:42:39+00:00Added an answer on June 4, 2026 at 10:42 pm

    Try the following:

    (?<=(?<!_)__)([^_])(?=__(?!_))
    

    Examples:

    >>> regex = re.compile(r'(?<=(?<!_)__)([^_])(?=__(?!_))')
    >>> regex.findall('__a__ ')
    ['a']
    >>> regex.findall('___a___')
    []
    >>> regex.findall('__a__d___b___e__c__')
    ['a', 'c']
    >>> regex.findall('__a__c__')
    ['a', 'c']
    

    You said you wanted overlapping matches, but if you would not like the c to match in __a__c__, use the following (which was my original answer):

    (?<!_)__([^_])__(?!_)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regex that also matches Chinese, Greek, Russian, ... letters. What I
In my Python application, I need to write a regular expression that matches a
I need to use a regex will determine if a single line of python
I need to extract parts of a string using regex in Python. I'm good
I need a regex which will detect multiple occurrences of the new line character
I need a regex which will do the following Extract all strings which starts
I need a regex to match expressions which contain the string OKAY then a
Currently, I have the following Python regex: r'^https?://(www.)?domain.com/?(?P<path>.*)/?$' That I'm replacing with: r'/\g<path>/' This
I need to extract text from any kind of web page using regex in
I'm trying to extend the python.lang file so that it will make methods like

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.