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
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
Share on other sites

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.