Jump to content

Recommended Posts

I suck at regex! i need to pull just the number after "[License Number:" and between "-Expiration Date:11/19/2018-State Issued:CA]" in the below, how would I do that. 

[License Number:AG013440-Expiration Date:11/19/2018-State Issued:CA]

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/305598-regex-to-pull-just-the-number/
Share on other sites

Hmm, pretty sure that is an inefficient RegEx. It is using the greedy '*' along with the lazy modifier '?'. That causes the matching to do some recursive logic to get the result.

 

I think a better solution would be this

 

/License Number:([^-]*)/s

 

This is a strait-forward, efficient matching that will pull every character following 'License Number:' up to, but not including, the '-' character. Yes, you could just put a colon instead of the full 'License Number:' string, but it will be patently obvious to anyone else reading the code (or even to you if you have to go back to it in a few months).

  • 3 months later...

I have made some Regex to match UK number plates...shouldn't take much to make it suit your needs...

 

 

$this->regexPatterns[] = array( 'name' => "1to31to31", 'pattern' => "/^([A-Z]{1,3})(\s)?(\d{1,3}[A-Z]{1})$/", 'desc' => "Contains between 1 and 3 letters followed by an optional space and between 1 and 3 numbers followed by a single letter" );
        
        // (A1ABC) (A12ABC) (A123ABC) (A1 ABC) (A12 ABC) (A123 ABC)
        $this->regexPatterns[] = array( 'name' => "11to31to3", 'pattern' => "/^([A-Z]{1}\d{1,3})(\s)?([A-Z]{3})$/", 'desc' => "Contains 1 letter followed by between 1 to 3 numbers followed by optional space and then 3 letters" );
        
        // (1A) (12AB) (123ABC) (1 A) (12 AB) (123 ABC)
        $this->regexPatterns[] = array( 'name' => "1to31to3", 'pattern' => "/^([A-Z]{1,3})(\s)?(\d{1,3})$/", 'desc' => "Contains between 1 and 3 letters, followed an optional space and then between 1 and 3 numbers" );
        
        // (A1) (AB12) (ABC123) (A 1) (AB 12) (ABC 123)
        $this->regexPatterns[] = array( 'name' => "1to31to3", 'pattern' => "/^(\d{1,3})(\s)?([A-Z]{1,3})$/", 'desc' => "Contains between 1 and 3 numbers followed by an optional space and between 1 and 3 letters" );
        
        // (AB12 ABC)
        $this->regexPatterns[] = array( 'name' => "223", 'pattern' => "/^([A-Z]{2}\d{2})(\s)?([A-Z]{3})$/", 'desc' => "Contains 2 letters followed by 2 numbers an optional space and then 3 letters" );
        
        // (AB12 ABC)
        $this->regexPatterns[] = array( 'name' => "34", 'pattern' => "/^([A-Z]{3})(\s)?(\d{4})$/", 'desc' => "Contains 3 letters followed an optional space and then 4 numbers" );

From:

 

 

[License Number:AG013440-Expiration Date:11/19/2018-State Issued:CA]

 

To:

 

Array
(
[0] => License Number:AG013440
[1] => Expiration Date:11/19/2018
[2] => State Issued:CA
)
Array
(
[0] => License Number
[1] => AG013440
)
<h1>AG013440</h1>
Array
(
[0] => AG013440
[1] => AG
[2] => 013440
)
<h1>The numbers: 013440</h1>

 

Code:

 

<?php

$startString = "[License Number:AG013440-Expiration Date:11/19/2018-State Issued:CA]";

$test1 = explode("-", $startString);

print_r($test1);

$plate = explode(":", $test1[0]);

print_r($plate);

echo "<h1>{$plate[1]}</h1>\n";

$test2 = preg_match("/^([A-Za-z]{2})([0-9]{1,10})$/", $plate[1], $matches);

print_r($matches);

echo "<h1>The numbers: {$matches[2]}</h1>\n";

This thread is several months old. Besides, that is a mess of code. If you want to use RegEx - then create one RegEx to return all the parts of the data. Using explode() multiple times is not necessary. The OP was only wanting the license number, which is what I provided. However, in retrospect, I should have provided a simple substr() solution. But, if he did want all the parts of the data, I would have provided this:

$regEx = "#License Number\[^\-]*)\-Expiration Date\[^\-]*)\-State Issued\[^\]]*)#";
preg_match($regEx, $input, $results);
print_r($results);

Output

Array
(
    [0] => License Number:AG013440-Expiration Date:11/19/2018-State Issued:CA
    [1] => AG013440
    [2] => 11/19/2018
    [3] => CA
)
Edited by Psycho
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.