background preloader


Facebook Twitter

Tools - Roy Osherove - Team Leadership, Agile Development & .NET - Speaking, Consulting, Training and Tools. Advanced Regular Expression Tips and Techniques. Twice a month, we revisit some of our readers’ favorite posts from throughout the history of Nettuts+.

Advanced Regular Expression Tips and Techniques

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. Regex Powertoy (interactive regular expressions) 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.

Using Regular Expressions in PHP and JavaScript - O'Reilly Answers

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. For instance, an asterisk (*) has a meaning similar to what you have seen if you use a shell or Windows Command prompt (but not quite the same). 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. /Le +Guin/ Fuzzy character matching. Reggy - Home. A Ruby regular expression editor and tester.

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.

Regular Expressions - a Simple User Guide

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. Extended Regular Expressions (EREs) will support Basic Regular Expressions (BREs are essentially a subset of EREs). Most applications, utilities and laguages that implement RE's, especially PERL, extend the capabilities defined and this has become, mostly, the de facto standard.

The appropriate documentation should always be consulted. Translation: The page has been translated into Bulgarian, courtesy of Albert Ward - thanks. Contents The title is deceptive. Some Definitions before we start More Stuff. Do-it-with-sed. Regexxer Search Tool. TextCrawler - Search and Replace across files - Freeware. RegExr: Online Regular Expression Testing Tool. RegexBuddy: Learn, Create, Understand, Test, Use and Save Regular Expression. Reggy - Google Code. 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.

The Regex Coach - interactive regular expressions

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. It can simulate Perl's split and s/// (substitution) operators. Contents Download and installation The Regex Coach together with this documentation can be downloaded from The current version is 0.9.2 - see the changelog for what's new.

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! License. Perl 5.10 Advanced Regular Expressions.

Regular Expressions Cheat Sheet.