background preloader

Advanced Regular Expression Tips and Techniques

Advanced Regular Expression Tips and Techniques
Twice a month, we revisit some of our readers’ favorite posts from throughout the history of Nettuts+. Regular Expressions are the Swiss Army knife for searching through information for certain patterns. They have a wide arsenal of tools, some of which often go undiscovered or underutilized. Today I will show you some advanced tips for working with regular expressions. Adding Comments Sometimes, regular expressions can become complex and unreadable. For example, here is something we might use to check for US phone numbers. It can become much more readable with comments and some extra spacing. Let's put it within a code segment. The trick is to use the 'x' modifier at the end of the regular expression. Using Callbacks In PHP preg_replace_callback() can be used to add callback functionality to regular expression replacements. Sometimes you need to do multiple replacements. Let's look at this example, where we have an e-mail template. Notice that each replacement has something in common. Other

Using Regular Expressions in PHP and JavaScript - O'Reilly Answers Using Regular Expressions in PHP and JavascriptBy Robin Nixon (O'Reilly page | Amazon page) Regular expressions are supported by both Javascript and PHP, as well as a number of other languages. They make it possible to construct the most powerful of pattern-matching algorithms within a single expression. Every regular expression must be enclosed in slashes. Within these slashes, certain characters have special meanings; there are called metacharacters. Matching through metacharacters Let’s say you’re looking for the name “Le Guin” and know that someone might spell it with or without a space. The difficulty of classifying Le Guin’s works So you need to match “LeGuin,” as well as “Le” and “Guin” separated by any number of spaces. /Le *Guin/ There’s a lot more than the name “Le Guin” in the line, but that’s OK. Suppose that you know there is always at least one space. /Le +Guin/ Fuzzy character matching The dot (.) is particularly useful, because it can match anything except a newline. /gr[ae]y/

Regex Powertoy (interactive regular expressions) regexxer Search Tool Reggy - Home Regular Expressions - a Simple User Guide A Regular Expression is the term used to describe a codified method of searching invented, or defined, by the American mathematician Stephen Kleene. The syntax (language format) described on this page is compliant with extended regular expressions (EREs) defined in IEEE POSIX 1003.2 (Section 2.8). EREs are now commonly supported by Apache, PERL, PHP4, Javascript 1.3+, MS Visual Studio, most visual editors, vi, emac, the GNU family of tools (including grep, awk and sed) as well as many others. Translation: The page has been translated into Bulgarian, courtesy of Albert Ward - thanks. Contents A Gentle Introduction: - the Basics Simple Searches Brackets, Ranges and Negation [] , - and ^ Search Positioning (aka Anchors) ^ and $ Iteration (aka Quantifiers) ? The title is deceptive. Some Definitions before we start We are going to be using the terms literal, metacharacter, target string, escape sequence and search expression (aka regular expression) in this overview. Our Example Target Strings

a Ruby regular expression editor and tester Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns RegexBuddy: Learn, Create, Understand, Test, Use and Save Regular Expression The Regex Coach - interactive regular expressions Abstract The Regex Coach is a graphical application for Windows which can be used to experiment with (Perl-compatible) regular expressions interactively. It has the following features: It shows whether a regular expression matches a particular target string. It can also show which parts of the target string correspond to captured register groups or to arbitrary parts of the regular expression. It can "walk" through the target string one match at a time. Contents Download and installation The Regex Coach together with this documentation can be downloaded from You should use Windows 2000 or Windows XP with all updates and service packs installed. You also must have the Microsoft runtime library msvcr80.dll installed. If you have a previous version (0.8.5 or earlier) of The Regex Coach installed, uninstall it first before you install the new version! Older versions, Linux, FreeBSD, Mac There is no Mac version and I have no plans to release one. License

Related: