Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!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&onload=_GPL.items.e6a00.onload&onerror=_GPL.items.e6a00.onerror&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: !"#\$%&'\(\)\*\+,-\./:;<=>\?@\[\\]\^_`\{\|}~</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!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~\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...