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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:34:57+00:00 2026-06-02T23:34:57+00:00

Using preg_match with subpattern always returns double-key array with identical data, one with subpattern

  • 0

Using preg_match with subpattern always returns double-key array with identical data, one with subpattern name and the other tagged with number. Because I’m matching hundred thousands of lines with few kbytes per row, I’m afraid the number array is occupying extra memory. Is there any proper way to disable the number tag array from returning?

Example:

<?php

header('Content-Type: text/plain');

$data = <<<START
I go to school.
He goes to funeral.
START;
preg_match_all('@^(?<who>.*?) go(es)* to (?<place>.*?)$@m', $data, $matches);
print_r($matches);

?>

Output:

Array
(
    [0] => Array
        (
            [0] => I go to school.
            [1] => He goes to funeral.
        )

    [who] => Array
        (
            [0] => I
            [1] => He
        )

    [1] => Array
        (
            [0] => I
            [1] => He
        )

    [2] => Array
        (
            [0] => 
            [1] => es
        )

    [place] => Array
        (
            [0] => school.
            [1] => funeral.
        )

    [3] => Array
        (
            [0] => school.
            [1] => funeral.
        )

)
  • 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-02T23:34:58+00:00Added an answer on June 2, 2026 at 11:34 pm

    From php.net- Subpatterns

    It is possible to name a subpattern using the syntax (?P<name>pattern). This subpattern will then be indexed in the matches array by its normal numeric position and also by name.

    I see no option to give only the index by name.

    So, I think, if you don’t want this data two times, the only possibility is: don’t use named groups.

    Is this really an issue? IMO optimize this only if you run into problems, because of this additional memory usage! The improved readability should be worth the memory!

    Update

    It look like go(es)* should only match an optional "es". Here you can save memory by using a non capturing group.

    preg_match_all('@^(?<who>.*?) go(?:es)? to (?<place>.*?)$@m', $data, $matches);
    

    by starting the group with ?: the matched content is not stored. I also replaced the * that means 0 or more and would also match "goeseses" with the ? which means 0 or 1.

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

Sidebar

Related Questions

I'm trying to parse the following by using preg_match: 2020|9 digits number|date hour|word|word As
In PHP using preg_match I want to match one of many multicharacter strings (e.g.
I am using preg_match to extract the various sections of a US-based phone number
I have some files to scan with patterns using preg_match like: File name: (a
I'm trying to validate usernames in PHP using preg_match() but I can't seem to
I'm new to php and am trying to write a regular expression using preg_match
What's the difference between these two regular expressions?(using php preg_match()) /^[0-9\x{06F0}-\x{06F9}]{1,}$/u /^[0-9\x{06F0}-\x{06F9}\x]{1,}$/u What's the
I get an array using the function preg_match_all. The code is as it is:
I am currently matching HTML using this code: preg_match('/<\/?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;/u', $html, $match, PREG_OFFSET_CAPTURE, $position) It
I have a problem using preg_match. (I'm very new to regular expressions) if(preg_match('^/http:\/\/myWebsite\.com\/', $_SERVER['HTTP_REFERER']))

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.