Regular Expressions

From JasonAntmanWiki
Jump to: navigation, search

Regular Expressions...

Contents

Introduction

See Wikipedia Regex

PHP HTML_QuickForm

This is how you would apply these rules in a form called form in the PEAR HTML_QuickForm package under PHP.

$form->registerRule('zip','regex','\d{5}(-\d{4})?');
$form->addRule('elementName','Please enter a valid Zip Code.','zip');

In this example, Zip is the name of the rule, and ElementName is the name of the element in form to apply the rule to. In this example, I am testing for a zip code. the regex, \d{5}(-\d{4})? will exaulate for any expression of the form XXXXX or XXXXX-XXXX where X is a numeric digit (base-10) 0-9.

Samples

In the Sample Expressions, a 0 (in bold) symbolizes any numeric digit 0-9. The upper case letterA (underlined) symbolizes any letter A-Z in upper case. The lower case letter a (underlined) symbolizes any letter a-z in lower case. The letter A (in bold) symbolizes any letter A-Z or a-z (regardless of case).

Zip Codes

Zip Or Zip+4:

RegEx:
\d{5}(-\d{4})?

Matches: 00000 or 00000-0000

MySQL Date

This matches a MySQL date in PHP form Y-m-d. RegEx: (19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01]) Matches: YYYY-MM-DD

Time

This matches a simple HH:MM 24-hour time format: RegEx: [0-2][0-9][:][0-5][0-9] Matches: HH:MM

External Links

[1] regular-expressions.info examples.

Views
Notice - this is a static HTML mirror of a previous MediaWiki installation. Pages are for historical reference only, and are greatly outdated (circa 2009).