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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:06:30+00:00 2026-06-01T22:06:30+00:00

I am trying to match multiple inputs in the same regex to capture the

  • 0

I am trying to match multiple inputs in the same regex to capture the number following.
If the input doesnt match the first group then I have issues because all previous capture groups are still captured despite it doesnt match they group. I believe I need to use a lookaround assertion, but I am not familiar with those.

Sample input:

wordA 123456
wordA: 123456
wordA : 123456
wordA R123465
wordA: R123456
wordA : R123456
wordB R123465

WordA has both optional : and R. So far I have this: /(?:wordA :?R?(\d+)|wordB R(\d+))/i.
Using the last sample input provides this result:

array
  0 => string 'wordB R123456' (length=13)
  1 => string '' (length=0)
  2 => string '123456' (length=6)

Wanted result is:

array
  0 => string 'wordB R123465' (length=13)
  1 => string '123456' (length=6)

Any ideas how to fix ?

  • 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-01T22:06:32+00:00Added an answer on June 1, 2026 at 10:06 pm

    The problem is your alternation

    /(?:wordA :?R?(\d+)|wordB R(\d+))/i
                  ^^^^^        ^^^^^
                 Group 1       Group 2
    

    So if your regex matches the second alternative, the result will be in group 2 (array[2]) and the first group will be empty.

    Change it to this

    (?:wordA :?R?|wordB R)(\d+)
    

    See it here on Regexr

    Then your number will always be in the first group (because there is only one)

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

Sidebar

Related Questions

I have a program that takes multiple files as input. What I'm trying to
I'm trying to make a MATCH of columns using multiple criteria, as in the
I'm trying to match the following items in the string pcode : u followed
I have multiple subdomains trying to use a single subdomain for authentiction using forms
I have the url: http://primarydomain.com/sites/secondarydomain/?foo=bar What regex expression could I use to match the
I am trying to filter an XML based on multiple parameters that I have
I'm trying to match a regex string against a data file in perl, but
I'm trying to match replace a multiple lines block with sed and I can't
I'm trying to use Java regexps to match a pattern that spans multiple lines.
I am trying to create a simplified search box that will match multiple columns

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.