Jump to content
tobeyt23

Regex to Pull just the number

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

Share this post


Link to post
Share on other sites

i got it

 $matches = array();
 preg_match('/:(.*?)\-/s', $license, $matches);
 $license =  $matches[1];

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites

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" );

Share this post


Link to post
Share on other sites

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";

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.