background preloader

Regex

Facebook Twitter

US and Canada zip code validation RegEx. Regexp Tutorial - Character Classes or Character Sets. With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. If you want to match an a or an e, use [ae]. You could use this in gr[ae]y to match either gray or grey. Very useful if you do not know whether the document you are searching through is written in American or British English.

A character class matches only a single character. gr[ae]y does not match graay, graey or any such thing. You can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9. Character classes are one of the most commonly used features of regular expressions. Negated Character Classes Typing a caret after the opening square bracket negates the character class. It is important to remember that a negated character class still must match a character. q[^u] does not mean: "a q not followed by a u". Make a Donation. Regular Expressions PHP Tutorial. I have searched the web far and near for a good tutorial on PHP Regular Expressions and I have come up with a multitude of sites. However, I needed just a little bit of information from each of the sites and I ended up trying to move between 10 different webpages to get the information I needed at a particular time.

This tutorial is a collation of all those bits of information. Some of this is my work, but it is mostly good collection of other tutorials available out there. In order to give authors credit for their work, I have included ALL the links of those pages and if anyone feels like this is an outrage, let me know and I will take down the relevant information. So here goes... Basic Syntax of Regular Expressions (as from PHPBuilder.com) Some useful PHP Keywords and their use (php.net man pages) preg_split preg_match Perl Style Delimiters (as from crazygrrl.com) When using Perl-style matching, the pattern also has to be enclosed by special delimiters.

/colou? ! /colou? Example: /\bhomer\b/ Regular Expression Quick Start. This quick start gets you up to speed quickly with regular expressions. Obviously, this brief introduction cannot explain everything there is to know about regular expressions. For detailed information, consult the regular expressions tutorial. Each topic in the quick start corresponds with a topic in the tutorial, so you can easily go back and forth between the two. Many applications and programming languages have their own implementation of regular expressions, often with slight and sometimes with significant differences from other implementations. When two applications use a different implementation of regular expressions, we say that they use different "regular expression flavors". This quick start explains the syntax supported by the most popular regular expression flavors.

Text Patterns and Matches A regular expression, or regex for short, is a pattern describing a certain amount of text. Literal Characters This regex can match the second a too. Learn more about literal characters q(? PHP Tutorials Examples Introduction to PHP Regex. By Kevin Waterson Contents What is a regex? At its most basic level, a regex can be considered a method of pattern matching or matching patterns within a string. In PHP the most oft used is PCRE or "Perl Compatible Regular Expressions". Here we will try to decypher the meaningless hieroglyphics and set you on your way to a powerful tool for use in your applications. Do not try to understand all this in a single sitting. Instead, take in a little and come back as you grasp various concepts.

Where do I begin? At the beginning. If we simply wanted to see if the pattern 'abc' was within our larger string we could easily do something like this: <? Echo preg_match ( "/abc/" , $string ); ? The code above will echo '1'. Match beginning of a string Now we wish to see if the string begins with abc. From the code above we see that it echo's the line The string begins with abc The forward slashes are a delimeter that hold our regex pattern. What if I want case insensitive? Meta characters . <? Still with us? <? Glossary - Regular Expressions.

PHP regular expressions examples. The regular expression, as a pattern, can match all kinds of text strings helping your application validate, compare, compute, decide etc. It can do simple or very complex string manipulations. The list of possibilities is enormous when it comes to what you can achieve using regular expressions. You can take any phrase that starts with an "A" or any character and do various things with it. You can match phone numbers, email addresses, url's, credit card numbers, social security numbers, zip codes, states, cities..... (the list never ends). A huge script that is supposed to validate a user input and prepare it for sql can be reduced to only one line with the help of preg_replace.

Mastering Regular Expressions quickly covers the basics of regular-expression syntax, then delves into the mechanics of expression-processing, common pitfalls, performance issues, and implementation-specific differences. The following function will return an array of all regex matches in a given string ($text):