CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: RegEx to Match various alpha-numeric strings  (Read 709 times)

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
RegEx to Match various alpha-numeric strings
« on: 20 Feb 2020, 05:43:09 pm »
Just when you thought it was safe - I pop up again!

I have been putting some of the things I have learnt into practice and have started a rewrite on the project I was working on, but came across this small problem,

Code: [Select]
// VRM Check(s)
$regexs = [
0 => "/^([a-z]{2}[0-9]{2})\s?([a-z]{3})$/", // AB12 ABC
1 => "/^([a-z]{1}[0-9]{1,3})\s?([a-z]{3})$/", // A1 ABC, A12 ABC, A123 ABC
2 => "/^([a-z]{3})\s?([0-9]{1,3}[a-z]{1})$/", // ABC 1A, ABC 21A, ABC 321
3 => "/^([a-z]{2})\s?([0-9]{4})$/", // AB 1234
4 => "/^([0-9]{4})\s?([a-z]{2})$/", // 1234 AB
//5 => "/^([a-z]{3})\s?([0-9]{3})$/", // ABC 123
6 => "/^([a-z]{3})\s?([0-9]{4})$/", // ABC 1234
7 => "/^([a-z]{1,3})\s?([0-9]{1,3})$/", // 1 A, 1 AB, 1 ABC, 12 A, 12 A, etc
8 => "/^([0-9]{1,3})\s?([a-z]{1,3})$/", // A 1, AB 1, ABC 1, A 12, AB 12, etc
];

foreach( $regexs as $regex ) {

if( preg_match( $regex, $_POST['vrm'], $matches ) ) {

break;

}

}

How do I make this leaner and cleaner?
« Last Edit: 21 Feb 2020, 03:21:06 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Dave

  • Junior Member
  • *
  • Posts: 38
  • Karma: +12/-0
Re: RegEx to Match various alpha-numeric strings
« Reply #1 on: 21 Feb 2020, 10:27:51 am »
Without context it's hard to tell what you're trying to ask. What is the input and expected output?
Dave

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: RegEx to Match various alpha-numeric strings
« Reply #2 on: 21 Feb 2020, 10:54:51 am »
Without context it's hard to tell what you're trying to ask. What is the input and expected output?
It is to:
  • validate the user input as a correct UK style registration number* such as AB12 ABC, A1 ABC, etc
  • format the registration number when it is retrieved back from the database
* It will accept even invalid charaters that are not used - as long as it follows the correct format "I" is not used... and Z is only used in Northern Ireland registration numbers
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

 

SMF spam blocked by CleanTalk

Advertisement