Jump to content

Need Help Badly In Creating A Ereg Pattern


oracle259

Recommended Posts

Hi, I need your help badly I am new to php and regular expression,, so please bear with me.

I need to know how do i create an eregi pattern that will ensure the following password match.

Password may start with a number, lower or upper case letter, but must include at least one number.

I tried this pattern

^[a-zA-Z0-9]{0,7}[0-9]*[a-zA-Z0-9]*$

But it is clearly weak as it will validate abcdefg5hi but not 1234567T890

Please help this is driving me nuts.
Link to comment
Share on other sites

I'm new at this stuff to, but I in one of my scripts I use the [i]preg_match()[/i] function in an if statement to search for a certain pattern of characters in some of my strings, so maybe it will work for you too. I'm still pretty bad with the expressions, character ranges, and quantifiers though. Sorry.
Link to comment
Share on other sites

its not weak at all!

that expression is doing exactly as it is told!!! ;)

what you have said in the regex is

match any string where first 7 chars are alphanumeric follwoed by any numer of numbers follwed by anynumber of alphanumeric characters.

the first string you have is abcdefg5hi
abcdefg - 7 alphanumeric
5 - any number of numers
hi - any number of alphanumeric.

your second string is 1234567T890
1234567 - 7 alphanumeric
T - FAILS HERE AS THIS IS NOT A NUMBER!
90 - any number of numbers

those are two completely different patterns that you are trying to match.

come back with a explanation of what you're trying to achieve......
Link to comment
Share on other sites

This regex will match a string with at least one number, being at least 7 characters long containing numbers or  letters. ie the string can contain only numbers.

If you'd like something else then you'll need to provide a better explanation as ToonMariner pointed out.
[code=php:0]
<?php
$strings = array();
$strings[] = '9Akkdlslsoei';
$strings[] = 'uhdiuehwdie';
$strings[] = 'dhs9';
$strings[] = '1234567T890';
$strings[] = 'abcdefg5hi';

foreach($strings as $string)
{
    if (!preg_match('/^(?=.*?[0-9])[a-zA-Z0-9]{7,}$/', $string))
    {
        print $string." is invalid <br />\n";
    }
    else
    {
        print $string." is valid <br />\n";
    }
}
?>
[/code]
Link to comment
Share on other sites

Ok i read some more tutorials on regular expressions and came up with this.

^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[\W])[a-zA-Z0-9\W]{7,}$

It's close to the final expression that i need but its weak in 1 key area. The 7-digit sequence may start with a number, which i dont want it to do.

If you guys have any ideas please let me know. Also if u have any tips on refining or optimizing the expression please let me know also.

Thanks in advance for your help.

Link to comment
Share on other sites

Alright i think I have got it. Please see the final expression

^[a-zA-Z]?(?=.*?[\d])(?=.*?[A-Z])(?=.*?[\W])[\w\W]{8,15}$

I ran i through the Regex Coach and it passed. However, if you guys see any weaknesses or know of a better way of achieving the same results please let me know.

Thanks
Link to comment
Share on other sites

[quote=oracle259]
^[a-zA-Z]?(?=.*?[\d])(?=.*?[A-Z])(?=.*?[\W])[\w\W]{8,15}$
[/quote]
With the pattern you posted, you're still allowed to have a number at the beginning because "[a-zA-Z]?" means that the [a-zA-Z] is optional.

If you remove the "?" at the end it'll be close to what you want, but because you have the lookaheads comming after the "[a-z][A-Z]" match at the beginning of the string the pattern won't match a string starting with and containing only one capital letter.

eg:
Aks#9sd9w

I think the following is closer to what you want
[code]
/^(?=.*?[\d])(?=.*?[A-Z])(?=.*?[\W])[a-zA-Z].{8,15}$/s
[/code]
Note that [\w\W] is effectively saying "Any Character" which is what "." is generally used for. Although the additional "s" modifier is needed to have it match newline characters. You can change it if you'd like.

EDIT: I should also mention that the password's length is currently required to be at least 9 characters long. You'll want to change the "8" in "{8,15}" to "6" if 7 is what you're going for.
Link to comment
Share on other sites

You could have done this with multiple if statements with far less complexity and more readability than a single regular expression.
[code]
if (
    strlen($password) <= 15
    && strlen($password) >= 8
    && preg_match('/[A-Z]/',$password)
    && preg_match('/[a-z]/',$password)
    && preg_match('/^[^0-9]/',$password)
    && preg_match('/\d/',$password)
    && preg_match('/\D|\W/',$password)
) { // ....
[/code]

In addition, you could split up those conditions into multiple if statements, allowing your program to inform the user of a specific error, i.e., "Password too short" or "Password may not start with a number."  Not to mention, this will make more sense when you come back to it in a few months....  :)
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.