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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:57:43+00:00 2026-05-28T17:57:43+00:00

How do I control whether parenthesis in my regular expression mark a sub group,

  • 0

How do I control whether parenthesis in my regular expression mark a sub group, do something else, or both?

For example if I have strings such as “AA12345” and “AB12345” and I want to preg_match for the first two letters which are always either AA or AB, I have:

preg_match('/(A(A|B)).*/',$string,$matches);

(I put the .* for the sake of this question because the rest of the string isn’t relevant)

With this setup, assuming $string=”AA12345″, I’m getting $matches =

Array
(
[0] => AA12345
[1] => AA
[2] => A
)

I don’t need or want the “[2] => A” as a result, but I can’t remove the parenthesis from the regex because they are needed for the OR operator. How do I deal with this? Just ignore the result, or is there a better way?

  • 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-28T17:57:44+00:00Added an answer on May 28, 2026 at 5:57 pm

    You can use a “non-capturing group” of the form (?:...):

    preg_match('/(A(?:A|B)).*/',$string,$matches);
    

    As the documentation puts it:

    If an opening parenthesis is followed by “?:”, the subpattern does not do any capturing, and is not counted when computing the number of any subsequent capturing subpatterns. For example, if the string “the white queen” is matched against the pattern the ((?:red|white) (king|queen)) the captured substrings are “white queen” and “queen”, and are numbered 1 and 2.

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

Sidebar

Related Questions

I would have my Gridview User Control In gray frame(its div tag) whether Gridview
What do I need to do to control whether my classes compiled with a
Does a custom control (ascx) get rendered regardless of whether visibility property is true
I'm wondering whether anyone has been able to use the Microsoft Chart control in
I have a custom control that implements IPostBackEventHandler. Some client-side events invoke __doPostBack(controlID, eventArgs).
I have control over the HttpServer but not over the ApplicationServer or the Java
I have a ListView control, and I'm trying to figure out the easiest/best way
I have an extender control that raises a textbox's OnTextChanged event 500ms after the
In XCode (4.1) is it possible to control whether or not a particular build
I need to know whether Control.BeginInvoke and Control.Invoke calls will execute in the order

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.