Jump to content

Optimizing Regex


oracle259

Recommended Posts

I'm new to php and regex, as such i have been practicing. But its not only important that i learn how to simply write these expressions i also need to learn how to optimize then.

I recently wrote this expression

^[A-Z]{1}([1]{4}|[2]{4}|[3]{4}|[5]{4}|[6]{4}|[7]{4}|[8]{4}|[9]{4})$

Its meant to validate a 5 character alphanumeric PIN that starts with an uppercase letter and 4 digits (no zeros). Now i know it works but its not very pretty as u can see. My question is this how can i optimize it.

Link to comment
https://forums.phpfreaks.com/topic/13883-optimizing-regex/
Share on other sites

[code]<?php

$tests = array(
'A1111',
'B2222',
'C3333',
'A1234',
'B2223',
'C3332',
);

foreach ($tests as $test) {
echo "<b>Testing '$test':</b>";
echo preg_match('/^[A-Z]([1-9])\1{3}$/', $test) ?
'Has 4 identical digits' :
'Does not have 4 identical digits' ;
echo '<br />';
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/13883-optimizing-regex/#findComment-54128
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.