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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:33:35+00:00 2026-06-14T21:33:35+00:00

I need to match the following sets of input: foo_abc_bar foo_bar and get abc

  • 0

I need to match the following sets of input:

foo_abc_bar  
foo_bar

and get “abc” or an empty string as the result.

So this is the regular expression I wrote:

r'foo_(abc|)[_|]bar'

But for some reason, this does not match with the second string that I have given.

On further inspection, I found that [_|] does not match an empty string.

So, how do I solve this problem?

  • 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-14T21:33:36+00:00Added an answer on June 14, 2026 at 9:33 pm

    To make abc_ optional, you could use the question mark operator:

    (abc_)?
    

    Thus, the entire regex becomes:

    r'foo_(abc_)?bar'
    

    With this regex, the second underscore (if present) will become part of the capture group. If you don’t want that, you could either remove it post-match with .rstrip('_') or use a slightly more complex regex:

    r'foo_(?:(abc)_)?bar'
    

    I found that [_|] does not match an empty string.

    That’s right. Square brackets denote a character group. The [_|] would match exactly one underscore or exactly one vertical bar, and nothing else. In other words, the vertical bar loses its special meaning when it appears inside a character group.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a regular expression which can match following specification for string
I need a regular expression for c# which can match following pattern abc1abcd 1abcdefg
i need a regular expression to match only the word's that match the following
I need to match the following line using regular expression. Single lines contain the
I need to match the following values and my current Regular Expression can't seem
I need to match the following string with regular expressions in Java: Hello: ${firstName}
Hi I need regular expression to match the following format in c# {productName}-{version}-{X}-of-{Y}.EXE Example
I need to match and remove all tags using a regular expression in Perl.
I am trying to match the following string in Javascript Regex PC123456 This is
I need to match a set of strings in Java. This string can contain

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.