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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:45:58+00:00 2026-05-16T04:45:58+00:00

Three underscore separated elements make my strings : – first (letters and digits) –

  • 0

Three underscore separated elements make my strings :
– first (letters and digits)
– middle (letters, digits and underscore)
– last (letters and digits)

The last element is optional.

Note : I need to access my groups by their names, not their indices.

Examples :

String : abc_def
first : abc
middle : def
last : None

String : abc_def_xyz
first : abc
middle: def
last: xyz

String : abc_def_ghi_jkl_xyz
first : abc
middle : def_ghi_jkl
last : xyz

I can’t find the right regex…

I have two ideas so far :

Optional group

(?P<first>[a-z]+)_(?P<middle>\w+)(_(?P<last>[a-z]+))?

But the middle group matches until the end of the string :

String : abc_def_ghi_jkl_xyz
first : abc
middle : def_ghi_jkl_xyz
last : vide

Using the ‘|’

(?P<first>[a-z]+)_(?P<middle>\w+)_(?P<last>[a-z]+)|(?P<first>[a-z]+)_(?P<middle>\w+)

This expression is invalid : first and middle groups are declared two times. I though I could write an expression reusing the matched group from the first part of the expression :

(?P<first>[a-z]+)_(?P<middle>\w+)_(?P<last>[a-z]+)|(?P=first)_(?P=middle)

The expression is valid, however strings with just a first and a middle like abc_def are not matched.

Note

These strings are actually parts of a path I need to match. It could be paths like :

  • /my/path/to/abc_def
  • /my/path/to/abc_def/
  • /my/path/to/abc_def/some/other/stuf
  • /my/path/to/abc_def/some/other/stuf/
  • /my/path/to/abc_def_ghi_jkl_xyz
  • /my/path/to/abc_def_ghi_jkl_xyz/
  • /my/path/to/abc_def_ghi_jkl_xyz/some/other/stuf
  • /my/path/to/abc_def_ghi_jkl_xyz/some/other/stuf/
  • …

Any idea to solve my problem solely with regular expressions ? Post-processing the matched groups is not an option.

Thank you very much !

  • 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-16T04:45:59+00:00Added an answer on May 16, 2026 at 4:45 am

    Change the middle group to be non-greedy, and add beginning and end-of-string anchors:

    ^(?P<first>[a-z]+)_(?P<middle>\w+?)(_(?P<last>[a-z]+))?$
    

    By default, the \w+will match as much as possible, which eats the rest of the string. Adding the ? tells it to match as little as possible.

    Thanks to Tim Pietzcker for pointing out the anchor requirements.

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

Sidebar

Ask A Question

Stats

  • Questions 507k
  • Answers 507k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer So you want to keep the DIV updated whenever a… May 16, 2026 at 4:05 pm
  • Editorial Team
    Editorial Team added an answer I don't think you should use the temp folder like… May 16, 2026 at 4:05 pm
  • Editorial Team
    Editorial Team added an answer Each instance of Control has one and only one window… May 16, 2026 at 4:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there any ready function which converts camel case Strings into underscore separated string?
I'm programming C++ using the underscore naming style (as opposed to camel case) which
I have a list of strings that all follow a format of parts of
Well, I have a really small problem. How can I access an ASP.NET Drop
I have a bunch of files that were named in a somewhat standard format.
I have a table with a field called 'user_car'. It consists of a cat'd
I have an DataMapper class which has some private and public methods. The DataMapper
We all know that this refers to the actual instance of a class...but it
How do I have a PowerShell script embedded within the same file as a
I like that vim 7.0 supports spell checking via :set spell, and I 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.