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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:39:09+00:00 2026-05-20T20:39:09+00:00

I’ve got a regular expression that I’m trying to match against the following types

  • 0

I’ve got a regular expression that I’m trying to match against the following types of data, with each token separated by an unknown number of spaces.

Update: “Text” can be almost any character, which is why I had .* initially. Importantly, it can also include spaces.

  1. Text
  2. Text 01
  3. Text 01 of 03
  4. Text 01 (of 03)
  5. Text 01-03

I’d like to capture “Text”, “01”, and “03” as separate groups, and all except “Text” are optional. The best I’ve been able to do so far is:

\s*(.*)\s+(\d+)\s*(?:\s*\(?\s*(?:of|-)\s*(\d+)\s*\)?\s*)

This matches #3-#5, and puts them in the proper capture groups. I can’t figure out, though, why when I add an additional ? to the end to make the part of the expression after 01 optional, my capture groups get all funky.

\s*(.*)\s+(\d+)\s*(?:\s*\(?\s*(?:of|-)\s*(\d+)\s*\)?\s*)?

The RegEx above matches #2-#5, but the capture groups are correct only for #2 and #5.

This seems like a straightforward regular expression, so I don’t know why I’m having so much trouble with it.

This is a link to an online RegEx evaluator I’m using to help me debug this: http://regexr.com?2tb64. The link already has the first RegEx and the test data filled in.

  • 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-20T20:39:09+00:00Added an answer on May 20, 2026 at 8:39 pm

    You didn’t say which regex tool you are using so I am assuming the least common denominator i.e. Javascript. Here is one that works:

    var re = /^\s*(.+?)(?:\s+(\d+)(?:(?:\s+\(?of\s+|-)(\d+)\)?)?)?$/i;
    

    To make this work in your Regexr tool, be sure to turn on the “multi-line option”.

    Here it the same thing in PHP syntax (with lots of juicy comments!):

    $re = '/ # Always write non-trivial regex in free-space mode!
        ^                  # Anchor to start of string.
        \s*                # optional leading whitspace is ok.
        (.+?)              # Text can be pretty much anything.
        (?:                # Group to allow applying ? quantifier
          \s+              # WS separates "Text" from first number.
          (\d+)            # First number.
          (?:              # Group to allow applying ? quantifier
            (?:            # Second number prefix alternatives
              \s+\(?of\s+  # Either " of 03" and " (of 03)",
            | -            # or just a dash  for "-03" case.
            )              # End second number prefix alternatives
            (\d+)          # Second number
            \)?            # Match ")" for " (of 03)" case.
          )?               # Second number is optional.
        )?                 # First numebr is optional.
        $                  # Anchor to start of string.
        /ix';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.