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

Regular Expression HOWTO — Python v2.7.2 documentation Introduction The re module was added in Python 1.5, and provides Perl-style regular expression patterns. Earlier versions of Python came with the regex module, which provided Emacs-style patterns. Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module. Regular expression patterns are compiled into a series of bytecodes which are then executed by a matching engine written in C. The regular expression language is relatively small and restricted, so not all possible string processing tasks can be done using regular expressions. Simple Patterns We’ll start by learning about the simplest possible regular expressions. For a detailed explanation of the computer science underlying regular expressions (deterministic and non-deterministic finite automata), you can refer to almost any textbook on writing compilers. Matching Characters \d \s \w Repeating Things Grouping

Regular Expressions Reference - Basic Syntax The regular expressions reference on this website functions both as a reference to all available regex syntax and as a comparison of the features supported by the regular expression flavors discussed in the tutorial. The reference tables pack an incredible amount of information. To get the most out of them, follow this legend to learn how to read them. The tables have six columns for each regular expression feature. The final two columns indicate whether your two chosen regular expression flavors support this particular feature. When this legend says "all versions" or "no version", that means all or none of the versions of each flavor that are covered by the reference tables: For the .NET flavor, some features are indicated with "ECMA" or "non-ECMA". For the std::regex and boost::regex flavor there are additional indicators ECMA, basic, extended, grep, egrep, and awk. For the PCRE2 flavor, some replacement string features are indicated with "extended".

Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

Related: