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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:40:49+00:00 2026-06-17T13:40:49+00:00

Why the following regex: $regex = ‘/\b(V|E)?\d{1,2}? ?\d{3} ?\d{3}\b/i’; does not match all the

  • 0

Why the following regex: $regex = '/\b(V|E)?\d{1,2}? ?\d{3} ?\d{3}\b/i';
does not match all the input below

I did think that the this (V|E)?\d{1,2}? ? would made optional the letters, the first one or two number and the first space

INPUT

<?php

$sms = array(
    'test test test 11 111 111 test test test',
    'test test test 1 111 111 test test test',
    'test test test 111 111 test test test', // does not match
    'test test test test test test 11111111',
    'test test test 1111111 test test test',
    'test test test 111111 test test test', // does not match
    'test test test E11 111 111 test test test',
    'test test test V1 111 111 test test test',
    'test test test V111 111 test test test', // does not match
    'test test test V11111111 test test test',
    'test test test V1111111 test test test',
    'test test test E111111 test test test', // does not match
    'test test test V 11 111 111 test test test',
    'test test test V 1 111 111 test test test',
    'test test test E 111 111 test test test', // does not match
    'test test test V 11111111 test test test',
    'test test test V 1111111 test test test',
    'test test test V 111111 test test test', //does not match
    'test test test V11 111 111 test test test',
    'test test test V1 111 111 test test test',
    'test test test E111 111 test test test', //does not match
    'test test test V11111111 test test test',
    'V1111111 test test test  test test test',
    'test test test V111111 test test test', // does not match
);

$regex = '/\b(V|E)?\d{1,2}? ?\d{3} ?\d{3}\b/i';
$noMatches = 0;
$index = 0;
foreach($sms as $v) {
    $match = preg_match($regex, $v, $matches);



    if($match) {
        //print_r($matches);
        //echo "$v match!\n";
        //$matches++;
    }
    else {
        echo "$index - $v does NOT match!\n";
        $noMatches++;
    }
    $index++;
}
$total = count($sms);
echo "\n\nTotal: $total\nNo Matches: $noMatches\n";

OUTPUT

$ php test-regex.php 
2 - test test test 111 111 test test test does NOT match!
5 - test test test 111111 test test test does NOT match!
8 - test test test V111 111 test test test does NOT match!
11 - test test test E111111 test test test does NOT match!
14 - test test test E 111 111 test test test does NOT match!
17 - test test test V 111111 test test test does NOT match!
20 - test test test E111 111 test test test does NOT match!
23 - test test test V111111 test test test does NOT match!


Total: 24
No Matches: 8

EDIT:

Using mario suggestion the regex is now $regex = '/\b(V|E)?\d{0,2} ?\d{3} ?\d{3}\b/i';,
why in some cases, this regex does not capture the letter V or E

$output = array(
    'test test test E11 111 111 test test test' => 'E11 111 111',
    'test test test V1 111 111 test test test' => 'V1 111 111',
    'test test test V111 111 test test test' => 'V111 111',
    'test test test V11111111 test test test' => 'V11111111',
    'test test test V1111111 test test test' => 'V1111111',
    'test test test E111111 test test test' => 'E111111',
    'test test test V 11 111 111 test test test' => '11 111 111', // Missing Letter
    'test test test V 1 111 111 test test test' => '1 111 111', // Missing Leter
    'test test test E 111 111 test test test' => 'E 111 111',
    'test test test V 11111111 test test test' => '11111111', // Missing Letter
    'test test test V 1111111 test test test' => '1111111', // Missing Letter
    'test test test V 111111 test test test' => 'V 111111',
    'test test test V11 111 111 test test test' => 'V11 111 111',
    'test test test V1 111 111 test test test' => 'V1 111 111',
    'test test test E111 111 test test test' => 'E111 111',
    'test test test V11111111 test test test' => 'V11111111',
    'V1111111 test test test  test test test' => 'V1111111',
    'test test test V111111 test test test' => 'V111111',
    'V 1111111 test test test' => '1111111', // Missing Letter
    'test test test V 1111111 test test test' => '1111111', // Missing Letter
);
  • 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-17T13:40:51+00:00Added an answer on June 17, 2026 at 1:40 pm

    ? only is a quantifier after groups or literal chars or characters classes e.g.

    If ? occurs after another quantifier * or + and {n,m} it will just make the matching less greedy. Meaning the regex will try to match the least amount.

    So \d{1,2}? does not mean optional. It means match one or two, but prefer to match just one. You meant to write \d{0,2} instead.

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

Sidebar

Related Questions

I have the following regex: s/\\/\\\\/g This does a nice job of replacing all
The following regex is failing on me; [Aa][æ-åÆ-Å].* But this regex is not failing
I have the following Regex ^http\\\\://[a-zA-Z0-9\\\\-\\\\.]+\\\\.[a-zA-Z]{2,3}(/\\\\S*)?$; But I'm not sure that it's validating URLs
I have the following regex that allows any letter and number (and I think
I have the following regex: \[([^ -\]]+)( - ([^ -\]]+))+\] This match the following
Im using the following regex to match the string below, so far so good.
I currently have the following regex to match a date: ([012]?\d)[\/.-]([0123]?\d)[\/.-]([012]\d{3})\b This will match,
The following regex finds text between substrings FTW and ODP. /FTW(((?!FTW|ODP).)+)ODP+/ What does the
I have the following regex which validates dates: //are not both dates if (!methods._isDate(first[0].value)
Take for example the following regex match. preg_match('!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(/page-[0-9]+)?$!', 'publisher/news/1/2010-march:03-23/test_title/1/page-1', $matches); print_r($matches); It produces the

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.