-
Notifications
You must be signed in to change notification settings - Fork 1
/
regex.html
202 lines (201 loc) · 7.46 KB
/
regex.html
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
201
202
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/png" href="./favicons/16x16.png" sizes="16x16">
<link rel="icon" type="image/png" href="./favicons/32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="./favicons/96x96.png" sizes="96x96">
<title>Regular Expression Guide</title>
<style>
body {
font-family: Arial, monospace;
margin: 25px; color: #000000;
background-color: #ffffff;
}
h2 { color: #4682b4; }
span {
color: #ff0000;
background-color: #fcdad8;
}
table {
border-collapse: collapse;
margin: 25px;
border: 1px solid #b0c4de;
}
th { color: #384a73; }
th, td {
padding: 8px 15px;
max-width: 245px;
border: 1px solid #b0c4de; }
.category { color: #4682b4; }
.center { text-align: center; }
.regex {
font-family: Courier, "Roboto Mono", monospace;
font-size: 1em; color: #000000 !important;
background-color: #ffffff !important; }
text { background-color: #85ebe1; }
</style>
</head>
<body>
<h2>Regular Expression Guide</h2>
<p>
A regular expression (regex) is a composable search pattern for
matching sequences of characters in a string. You can use
metacharacters to specify conditions for individual characters and subsequences.
This guide introduces some basic metacharacters.
</p>
<p>
<b>Note:</b> You can search online to learn more about regexes. This web tool uses regexes as implemented in JavaScript.
</p>
<table>
<tr>
<th>Metacharacter</th>
<th>Definition/Usage</th>
<th style="min-width: 120px;">Sample Regex</th>
<th>Matched Substring</th>
</tr>
<tr>
<td colspan="4" class="category"><b>Character sets</b></td>
</tr>
<tr>
<td class="regex center">\d</td>
<td>Any digit (0-9)</td>
<td class="regex">\d days</td>
<td>I rested for <span>3 days</span>.</td>
</tr>
<tr>
<td class="regex center">\w</td>
<td>Any "word" character: letters (A-Z, a-z), digits (0-9), underscore (_)</td>
<td class="regex">\w\w</td>
<td><span>He</span> <span>ha</span>s <span>wo</span>n.</td>
</tr>
<tr>
<td class="regex center">[<text> </text>]</td>
<td>Specify a character set</td>
<td>
• <span class="regex">[eo]mit</span><br>
• <span class="regex">letter [A-Z]</span>
</td>
<td>
• <span>emit</span>, <span>omit</span>, transmit<br>
• the <span>letter A</span>, the letter a
</td>
</tr>
<tr>
<td class="regex center">[^<text> </text>]</td>
<td>Specify characters to exclude</td>
<td>
<span class="regex">[^eo]mit</span><br>
</td>
<td>
emit omit tran<span>smit</span>
</td>
</tr>
<tr>
<td class="regex center">.</td>
<td>Any character except the newline character (\n)</td>
<td class="regex">Boeing.\w</td>
<td>
• <span>Boeing A</span>23<br>
• <span>Boeing-A</span>23<br>
• <span>BoeingA2</span>3
</td>
</tr>
<tr>
<td colspan="4" class="category"><b>Boundaries</b></td>
</tr>
<tr>
<td class="regex center">\b</td>
<td>Boundary between a \w character and a non-\w character (or start/end of string)</td>
<td class="regex">\b\w\w\b</td>
<td>She ate <span>an</span> apple.</td>
</tr>
<tr>
<td class="regex center">^</td>
<td>Start of string</td>
<td class="regex">^good</td>
<td><span>good</span> good good</td>
</tr>
<tr>
<td class="regex center">$</td>
<td>End of string</td>
<td class="regex">good$</td>
<td>good good <span>good</span></td>
</tr>
<tr>
<td colspan="4" class="category"><b>Quantifiers</b></td>
</tr>
<tr>
<td class="regex center"><text> </text>?</td>
<td>0 or 1</td>
<td class="regex">trees?</td>
<td>one <span>tree</span>, two <span>trees</span></td>
</tr>
<tr>
<td class="regex center"><text> </text>+</td>
<td>1 or more</td>
<td class="regex">[a-z]+</td>
<td>I <span>donated</span> <span>to</span> UNICEF <span>today</span>.</td>
</tr>
<tr>
<td class="regex center"><text> </text>*</td>
<td>0 or more</td>
<td class="regex">take.*away</td>
<td><span>takeaway</span>, <span>take the dog away</span></td>
</tr>
<tr>
<td class="regex center"><text> </text>{ }</td>
<td>Specify a quantity or quantity range</td>
<td>
• <span class="regex">[a-z]{3}</span><br>
• <span class="regex">[a-z]{2,4}</span><br>
• <span class="regex">[a-z]{2,}</span>
</td>
<td>
• a, an, <span>the</span>, <span>thi</span>s, <span>the</span>se<br>
• a, <span>an</span>, <span>the</span>, <span>this</span>, <span>thes</span>e<br>
• a, <span>an</span>, <span>the</span>, <span>this</span>, <span>these</span>
</td>
</tr>
<tr>
<td colspan="4" class="category"><b>Other</b></td>
</tr>
<tr>
<td class="regex center">(?:<text> </text>)</td>
<td>Treat a sequence as a (non-capturing) group</td>
<td class="regex">(?:da){2}</td>
<td>data, tada, <span>dada</span></td>
</tr>
<tr>
<td class="regex center"><text> </text>|<text> </text></td>
<td>Separate 2 or more sequences as alternatives</td>
<td>
• <span class="regex">one|two|three</span><br>
• <span class="regex">(?:one|two) days?</span>
</td>
<td>
• <span>one</span> dog, <span>two</span> cats, <span>three</span> mice<br>
• Stay <span>one day</span> or <span>two days</span>.
</td>
</tr>
<tr>
<td class="regex center">\<text> </text></td>
<td>
Escape a metacharacter so it is treated literally<br><br>
<b>Note:</b> The backslash itself also needs to be escaped to be treated literally.
</td>
<td>
• <span class="regex">1.5</span><br>
• <span class="regex">1\.5</span><br>
• <span class="regex">C:\\Documents</span>
</td>
<td>
• <span>1-5</span>, <span>1.5</span>, <span>1a5</span><br>
• 1-5, <span>1.5</span>, 1a5<br>
• <span>C:\Documents</span>\test.txt
</td>
</tr>
</table>
<br>
</body>
</html>