?
Solved

PHP Regular Expression

Posted on 2012-08-15
10
Medium Priority
?
464 Views
Last Modified: 2012-08-15
Hello,
Can someone code the regular expression for these field restrictions:

Must be between 4 and 20 characters long and should contain only letters, numbers, and the underscore symbol.

Thanks!
0
Comment
Question by:christamcc
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 4

Expert Comment

by:DustinKikuchi
ID: 38297049
preg_match('/^[\w\d_]{4,20}$/',$field);

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38297065
Case-insensitive match.  Remove the "i" if you want only uppercase.
preg_match('/^[A-Z0-9_]{4,20}$/i',$field);

Open in new window

HTH, ~Ray
0
 
LVL 4

Expert Comment

by:DustinKikuchi
ID: 38297084
Ray's is the one you'll want to use.  Used \w in my haste which will match spaces, tabs, etc.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38297091
Used \w in my haste which will match spaces, tabs, etc.
No, "\w" matches [a-zA-Z0-9_]. "\s" matches spaces, tabs, and other whitespace  = )
0
 
LVL 4

Expert Comment

by:DustinKikuchi
ID: 38297112
This is what I get for answering a regex question when I haven't used them in awhile :(

preg_match('/^[\w]{4,20}$/',$field);

Open in new window


Does appear to work as expected when testing.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38297349
According to my notes, \w matches any "word" which would be an unbroken string of characters that are not punctuation or whitespace or other word boundaries matched by \b.  As a practical matter I like to write regular expressions (and test them) using the techniques described in this article.  Writing them one-line-at-a-time with comments helps me keep my thinking clear.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

Following my own advice: http://www.laprbass.com/RAY_temp_christamcc.php
<?php // RAY_temp_christamcc.php
error_reporting(E_ALL);
echo '<pre>';


// THE REGULAR EXPRESSION
$rgx
= '/'           // REGEX DELIMITER
. '^'           // AT START OF STRING
. '['           // DEFINE A CHARACTER CLASS
. 'A-Z0-9_'     // CLASS INCLUDES LETTERS, NUMBERS, UNDERSCORE
. ']'           // END CHARACTER CLASS
. '{4,20}'      // MUST MATCH LENGTH FROM FOUR TO TWENTY
. '$'           // AT END OF STRING
. '/'           // REGEX DELIMITER
. 'i'           // CASE-INSENSITIVE
;

// THE TEST CASES (ADD YOUR OWN HERE)
$dats
= array
( 'HelloWorld'
, 'Four.Twenty'
, '1___5___10___15___20'
, 'Too short'
, 'a'
, 'Very_very_much_too_long'
, ' NeedsATrim'
)
;

// MAKE THE AUTOMATED TESTS AND REPORTS
foreach ($dats as $dat)
{
    echo "TESTING: $dat";

    if (preg_match($rgx, $dat))
    {
        echo ' MATCHED';
    }
    else
    {
        echo ' DID NOT MATCH';
    }
    echo PHP_EOL;
}

Open in new window

HTH, ~Ray
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38298084
My understanding is that in most regex flavours, \w is identical to [a-zA-Z0-9_]

My understanding of \b is that it essentially matches the (zero-length) gap between \w characters and non-\w characters (\W if you prefer), as well as the start and end of the string, which is exactly equivalent to this:
(?:(?<!\w)(?=\w)|(?<=\w)(?!\w))

Open in new window

It's unfortunate in a lot of situations that the ' character isn't included, because \b\w+\b fails to treat cases like "don't" and "O'Connor" as  single words.

Anyway, the most concise way to meet the author's requirements is:
preg_match('/^\w{4,20}$/',$field);

Open in new window

0
 

Author Closing Comment

by:christamcc
ID: 38298232
Thanks for you due diligence!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38298482
According to my notes, \w matches any "word"
Ray, I agree with Terry: "\w" matches any word character, not word. You could get a "word" by using "\w+". See "Shorthand Character Classes" for more information.

The reality is that all three approaches (DustinKikuchi, Ray's, and Terry's) are exactly equivalent. While I acknowledge that Ray took the extra time to document the regex, I argue that if one takes the time to understand exactly what "\w" means, then that extra documentation is unnecessary. (Admittedly it is helpful for persons unfamiliar with regex coming behind the original author.)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38298806
@Kaufmed: That is a good link for the Shorthand Character Classes.  I forget about this site sometimes.  It deserves to be remembered!
http://www.regular-expressions.info/charclass.html

And you're right about \w versus \w+ with the latter matching more than one alpha-numeric.  But in a character class expression that also includes explicit lengths I did not see any difference in my tests.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question