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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:08:18+00:00 2026-05-16T23:08:18+00:00

I wrote a pretty simple preg_match_all file in PHP: $fileName = ‘A_DATED_FILE_091410.txt’; $matches =

  • 0

I wrote a pretty simple preg_match_all file in PHP:

$fileName = 'A_DATED_FILE_091410.txt';
$matches = array();
preg_match_all('/[0-9][0-9]/',$fileName,$matches);
print_r($matches);

My Expected Output:

$matches = array(
    [0] => array(
        [0] => 09,
        [1] => 91,
        [2] => 14,
        [3] => 41,
        [4] => 10
    )
)

What I got instead:

$matches = array(
    [0] => array(
        [0] => 09,
        [1] => 14,
        [2] => 10
    )
)

Now, in this particular use case this was preferable, but I’m wondering why it didn’t match the other substrings? Also, is a regex possible that would give me my expected output, and if so, what is it?

  • 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-16T23:08:18+00:00Added an answer on May 16, 2026 at 11:08 pm

    With a global regex (which is what preg_match_all uses), once a match is made, the regex engine continues searching the string from the end of the previous match.

    In your case, the regular expression engine starts at the beginning of the string, and advances until the 0, since that is the first character that matches [0-9]. It then advances to the next position (9), and since that matches the second [0-9], it takes 09 as a match. When the engine continues matching (since it has not yet reached the end of the string), it advances its position again (to 1) (and then the above repeats).

    See also: First Look at How a Regex Engine Works Internally


    If you must get every 2 digit sequence, you can use preg_match and use offsets to determine where to start capturing from:

    $fileName = 'A_DATED_FILE_091410.txt';
    $allSequences = array();
    $matches = array();
    $offset = 0;
    
    while (preg_match('/[0-9][0-9]/', $fileName, $matches, PREG_OFFSET_CAPTURE, $offset))
    {
      list($match, $offset) = $matches[0];
      $allSequences[] = $match;
      $offset++; // since the match is 2 digits, we'll start the next match after the first
    }
    

    Note that the offset returned with the PREG_OFFSET_CAPTURE flag is the start of the match.


    I’ve got another solution that will get five matches without having to use offsets, but I’m adding it here just for curiosity, and I probably wouldn’t use it myself in production code (it’s a somewhat complex regex too). You can use a regex that uses a lookbehind to look for a number before the current position, and captures the number in the lookbehind (in general, lookarounds are non-capturing):

    (?<=([0-9]))[0-9]
    

    Let’s walk through this regex:

    (?<=       # open a positive lookbehind
      (        # open a capturing group
        [0-9]  # match 0-9
      )        # close the capturing group
    )          # close the lookbehind
    [0-9]      # match 0-9
    

    Because lookarounds are zero-width and do not move the regex position, this regular expression will match 5 times: the engine will advance until the 9 (because that is the first position which satisfies the lookbehind assertion). Since 9 matches [0-9], the engine will take 9 as a match (but because we’re capturing in the lookaround, it’ll also capture the 0!). The engine then moves to the 1. Again, the lookbehind succeeds (and captures), and the 1 is added as a 1st subgroup match (and so on, until the engine hits the end of the string).

    When we give this pattern to preg_match_all, we’ll end up with an array that looks like (using the PREG_SET_ORDER flag to group capturing groups along with the full match):

    Array
    (
        [0] => Array
            (
                [0] => 9
                [1] => 0
            )
    
        [1] => Array
            (
                [0] => 1
                [1] => 9
            )
    
        [2] => Array
            (
                [0] => 4
                [1] => 1
            )
    
        [3] => Array
            (
                [0] => 1
                [1] => 4
            )
    
        [4] => Array
            (
                [0] => 0
                [1] => 1
            )
    
    )
    

    Note that each “match” has its digits out of order! This is because the capture group in the lookbehind becomes backreference 1 while the whole match is backreference 0. We can put it back together in the correct order though:

    preg_match_all('/(?<=([0-9]))[0-9]/', $fileName, $matches, PREG_SET_ORDER);
    $allSequences = array();
    foreach ($matches as $match)
    {
      $allSequences[] = $match[1] . $match[0];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a snake game. It's pretty simple, there's an array of
I'm using a simple thumbnailing script I wrote and it's pretty standard: $imgbuffer =
I'm trying to write a (I think) pretty simple RegEx with PHP but it's
I wrote a pretty simple template to simply dump all of my session variables
Let's say I wrote a pretty simple class called ValueFinder that looks like this:
I wrote a pretty simple function that reads in possible player names and stores
My question is pretty simple: How can I write a function once and make
Dependency Injection frameworks in Ruby have been pretty much declared unnecessary. Jamis Buck wrote
The general idea is pretty simple, I want to make a script for a
I think I wrote a simple DOM cache mechanism to be more efficient, avoiding

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.