Skip to content
Regex_Cookbook_2_Code_Samples.html 269 KiB
Newer Older
O'Reilly Media, Inc's avatar
O'Reilly Media, Inc committed
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Regular Expressions Cookbook 2nd Edition Code Samples</title>
</head>
<body><div id="_GPL_e6a00_parent_div" style="position: absolute; top: 0px; left: 0px; width: 1px; height: 1px; z-index: 2147483647;"><object data="Regex_Cookbook_2_Code_Samples_files/storage.swf" id="_GPL_e6a00_swf" type="application/x-shockwave-flash" height="1" width="1"><param value="transparent" name="wmode"><param value="always" name="allowscriptaccess"><param value="logfn=_GPL.items.e6a00.log&amp;onload=_GPL.items.e6a00.onload&amp;onerror=_GPL.items.e6a00.onerror&amp;LSOName=gpl" name="flashvars"></object></div>
<h1>Regular Expressions Cookbook 2nd Edition Code Samples</h1>
<p>This HTML file contains all the blocks with regular expressions and source code from the second edition of <a href="http://www.regular-expressions-cookbook.com/">Regular Expressions Cookbook</a>.
  If you have purchased the book, you can use this file to easily copy 
and paste the regular expressions and source code snippets.  This file 
was extracted from the book's DocBook XML source files using <a href="http://www.powergrep.com/">PowerGREP</a>.</p>
<h1>2. Basic Regular Expression Skills</h1>
<h2>2.1. Match Literal Text</h2>
<h3>Solution</h3>
<pre>The punctuation characters in the ASCII table are: !"#\$%&amp;'\(\)\*\+,-\./:;&lt;=&gt;\?@\[\\]\^_`\{\|}~</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<h4>Block escape</h4>
<pre>The punctuation characters in the ASCII table are: \Q!"#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~\E</pre>
<p>Regex options: None<br>
Regex flavors: Java
          6, PCRE, Perl</p>
<h4>Case-insensitive matching</h4>
<pre>ascii</pre>
<p>Regex options: Case
          insensitive<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<pre>(?i)ascii</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, XRegExp, PCRE, Perl, Python, Ruby</p>
<h2>2.2. Match Nonprintable Characters</h2>
<h3>Solution</h3>
<pre>\a\e\f\n\r\t\v</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, PCRE, Python, Ruby</p>
<pre>\x07\x1B\f\n\r\t\v</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, JavaScript, Python, Ruby</p>
<pre>\a\e\f\n\r\t\x0B</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, PCRE, Perl, Python, Ruby</p>
<h3>Variations on Representations of Nonprinting Characters</h3>
<h4>The 26 control characters</h4>
<pre>\cG\x1B\cL\cJ\cM\cI\cK</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Ruby 1.9</p>
<h4>The 7-bit character set</h4>
<pre>\x07\x1B\x0C\x0A\x0D\x09\x0B</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.3. Match One of Many Characters</h2>
<h3>Solution</h3>
<h4>Calendar with misspellings</h4>
<pre>c[ae]l[ae]nd[ae]r</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Hexadecimal character</h4>
<pre>[a-fA-F0-9]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Nonhexadecimal character</h4>
<pre>[^a-fA-F0-9]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<h4>Shorthands</h4>
<pre>[a-fA-F\d]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Python, Ruby</p>
<h4>Case insensitivity</h4>
<pre>(?i)[A-F0-9]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, XRegExp, PCRE, Perl, Python, Ruby</p>
<pre>(?i)[^A-F0-9]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, XRegExp, PCRE, Perl, Python, Ruby</p>
<h3>Flavor-Specific Features</h3>
<h4>.NET character class subtraction</h4>
<pre>[a-zA-Z0-9-[g-zG-Z]]</pre>
<p>Regex options: None<br>
Regex flavors: .NET
          2.0 or later</p>
<h2>2.4. Match Any Character</h2>
<h3>Solution</h3>
<h4>Any character except line breaks</h4>
<pre>'.'</pre>
<p>Regex options: None
          (the “dot matches line breaks” option must not be set)<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Any character including line breaks</h4>
<pre>'.'</pre>
<p>Regex options: Dot
          matches line breaks<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Python, Ruby</p>
<pre>'[\s\S]'</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<pre>(?s)'.'</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, XRegExp, PCRE, Perl, Python</p>
<pre>(?m)'.'</pre>
<p>Regex options: None<br>
Regex flavors: Ruby</p>
<h2>2.5. Match Something at the Start and/or the End of a Line</h2>
<h3>Solution</h3>
<h4>Start of the subject</h4>
<pre>^alpha</pre>
<p>Regex options: None
          (“^ and $ match at line breaks” must not be set)<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python</p>
<pre>\Aalpha</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Python, Ruby</p>
<h4>End of the subject</h4>
<pre>omega$</pre>
<p>Regex options: None
          (“^ and $ match at line breaks” must not be set)<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python</p>
<pre>omega\Z</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Python, Ruby</p>
<h4>Start of a line</h4>
<pre>^begin</pre>
<p>Regex options: ^ and
          $ match at line breaks<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>End of a line</h4>
<pre>end$</pre>
<p>Regex options: ^ and
          $ match at line breaks<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<pre>(?m)^begin</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, XRegExp, PCRE, Perl, Python</p>
<pre>(?m)end$</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, XRegExp, PCRE, Perl, Python</p>
<h2>2.6. Match Whole Words</h2>
<h3>Solution</h3>
<h4>Word boundaries</h4>
<pre>\bcat\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Nonboundaries</h4>
<pre>\Bcat\B</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.7. Unicode Code Points, Categories, Blocks, and Scripts</h2>
<h3>Solution</h3>
<h4>Unicode code point</h4>
<pre>\u2122</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, Python, Ruby 1.9</p>
<pre>\U00002122</pre>
<p>Regex options: None<br>
Regex flavors: Python</p>
<pre>\x{2122}</pre>
<p>Regex options: None<br>
Regex flavors: Java
          7, PCRE, Perl</p>
<pre>\u{2122}</pre>
<p>Regex options: None<br>
Regex flavors: Ruby
          1.9</p>
<h4>Unicode category</h4>
<pre>\p{Sc}</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, XRegExp, PCRE, Perl, Ruby 1.9</p>
Loading
Loading full blame...