| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Regular Expressions Tutorial</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <table width=100% border=0 summary=""> |
| 10 | <tr> |
| 11 | <td align="center"> |
| 12 | <h4>Found at: http://publish.ez.no/article/articleprint/11/</h4> |
| 13 | </td> |
| 14 | </tr> |
| 15 | </table> |
| 16 | |
| 17 | <table width=100% border=0 summary=""> |
| 18 | <tr> |
| 19 | <td> |
| 20 | <h1>Regular Expressions Explained</h1> |
| 21 | </td> |
| 22 | </tr> |
| 23 | </table> |
| 24 | |
| 25 | <hr noshade="noshade" size=4> |
| 26 | |
| 27 | <br> |
| 28 | <table width=100% border=0 summary=""> |
| 29 | <tr> |
| 30 | <td> |
| 31 | <p class="byline">Author: Jan Borsodi</p> |
| 32 | </td> |
| 33 | <td align="right"> |
| 34 | <p class="byline">Publishing date: 30.10.2000 18:02</p> |
| 35 | </td> |
| 36 | </tr> |
| 37 | </table> |
| 38 | |
| 39 | <p> |
| 40 | This article will give you an introduction to the world of <i>regular |
| 41 | expressions</i>. I'll start off with explaining what regular expressions are |
| 42 | and introduce their syntax, then some examples with varying complexity. |
| 43 | |
| 44 | <p> |
| 45 | <h2>Concept</h2> |
| 46 | A <i>regular expression</i> is a text pattern consisting of a combination of |
| 47 | alphanumeric characters and special characters known as metacharacters. A |
| 48 | close relative is in fact the <i>wildcard expression</i> which are often used |
| 49 | in file management. The pattern is used to match against text strings. The |
| 50 | result of a match is either successful or not, however when a match is |
| 51 | successful not all of the pattern must match, this is explained later in the |
| 52 | article. |
| 53 | |
| 54 | <p> |
| 55 | You'll find that <i>regular expressions</i> are used in three different ways: |
| 56 | Regular text match, search and replace and splitting. The latter is basically |
| 57 | the same as the reverse match <i>i.e.</i> everything the <i>regular |
| 58 | expression</i> did not match. |
| 59 | |
| 60 | <p> |
| 61 | <i>Regular expressions</i> are often simply called regexps or RE, but for |
| 62 | consistency I'll be referring to it with its full name. |
| 63 | |
| 64 | <p> |
| 65 | Due to the versatility of the <i>regular expression</i> it is widely used in |
| 66 | text processing and parsing. UNIX users are probably familiar with them through |
| 67 | the use of the programs, <i>grep</i>, <i>sed</i>, <i>awk</i> and <i>ed</i>. |
| 68 | Text editors such as <i>(X)Emacs</i> and <i>vi</i> also use them heavily. |
| 69 | Probably the most known use of <i>regular expressions</i> are in the |
| 70 | programming language Perl; you'll find that Perl sports the most advanced |
| 71 | <i>regular expression</i> implementation to this day. |
| 72 | |
| 73 | <p> |
| 74 | <h2>Usage</h2> |
| 75 | Now you're probably wondering why you should bother to learn <i>regular |
| 76 | expressions</i>. Well if you're a normal computer user your benefits from |
| 77 | using them are somewhat small, however if you're either a developer or a system |
| 78 | administrator you'll find that knowing <i>regular expressions</i> will make |
| 79 | your (professional) life so much better. |
| 80 | |
| 81 | <p> |
| 82 | Developers can use them to parse text files, fix up code and other wonders. |
| 83 | System administrators can use them to search through logs, automate boring |
| 84 | tasks and sniff the network traffic for unauthorized activity. |
| 85 | |
| 86 | <p> |
| 87 | Actually I would go so far as to say it's a crime for a System Administrator |
| 88 | not to have <b>any</b> knowledge of <i>regular expressions</i>. |
| 89 | |
| 90 | <p> |
| 91 | <h2>Quantifiers</h2> |
| 92 | The contents of an expression are, as explained earlier, a combination of |
| 93 | alphanumeric characters and metacharacters. An alphanumeric character is either |
| 94 | a letter from the alphabet: |
| 95 | <p> |
| 96 | <table width=100% border=0 summary="Alphabetic Quantifiers"> |
| 97 | <tr> |
| 98 | <td bgcolor="#f0f0f0"> |
| 99 | <code>abc</code> |
| 100 | </td> |
| 101 | </tr> |
| 102 | </table> |
| 103 | <p> |
| 104 | or a number: |
| 105 | <p> |
| 106 | <table width=100% border=0 summary="Numeric Quantifiers"> |
| 107 | <tr> |
| 108 | <td bgcolor="#f0f0f0"> |
| 109 | <code>123</code> |
| 110 | </td> |
| 111 | </tr> |
| 112 | </table> |
| 113 | |
| 114 | <p> |
| 115 | Actually in the world of regular expressions any character which is not a |
| 116 | metacharacter will match itself (often called literal characters), however a |
| 117 | lot of the time you're mostly concerned with the alphanumeric characters. A |
| 118 | very special character is the backslash <b>\</b>, as this turns any |
| 119 | metacharacters into literal characters, and alphanumeric characters into a |
| 120 | sort of metacharacter or sequence. The metacharacters are: |
| 121 | <p> |
| 122 | <table width=100% border=0 summary="Metacharacters"> |
| 123 | <tr> |
| 124 | <td bgcolor="#f0f0f0"> |
| 125 | <code>\ | ( ) [ { ^ $ * + ? . < ></code> |
| 126 | </td> |
| 127 | </tr> |
| 128 | </table> |
| 129 | |
| 130 | <p> |
| 131 | With that said normal characters don't sound too interesting so let's jump to |
| 132 | our very first metacharacters. |
| 133 | |
| 134 | <p> |
| 135 | The punctuation mark, or dot, <b>.</b> needs explaining first since it often |
| 136 | leads to confusion. This character will not, as many might think, match the |
| 137 | punctuation in a line. It is instead a special metacharacter which matches any |
| 138 | character. Using this where you wanted to find the end of the line, or the |
| 139 | decimal in a floating number, will lead to strange results. As explained above, |
| 140 | you need to backslashify it to get the literal meaning. For instance take this |
| 141 | expression: |
| 142 | <p> |
| 143 | <table width=100% border=0 summary="Punctuation Marks"> |
| 144 | <tr> |
| 145 | <td bgcolor="#f0f0f0"> |
| 146 | <code>1.23</code> |
| 147 | </td> |
| 148 | </tr> |
| 149 | </table> |
| 150 | <p> |
| 151 | will match the number 1.23 in a text as you might have guessed, but it will |
| 152 | <b>also</b> match these next lines: |
| 153 | <p> |
| 154 | <table width=100% border=0 summary="Punction Marks"> |
| 155 | <tr> |
| 156 | <td bgcolor="#f0f0f0"> |
| 157 | <code>1x23</code><br> |
| 158 | <code>1 23</code><br> |
| 159 | <code>1-23</code> |
| 160 | </td> |
| 161 | </tr> |
| 162 | </table> |
| 163 | <p> |
| 164 | to make the expression <b>only</b> match the floating number we change it to: |
| 165 | <p> |
| 166 | <table width=100% border=0> |
| 167 | <tr> |
| 168 | <td bgcolor="#f0f0f0"> |
| 169 | <code>1\.23</code> |
| 170 | </td> |
| 171 | </tr> |
| 172 | </table> |
| 173 | |
| 174 | <p> |
| 175 | Remember this, it's very important. Now with that said we can get the show |
| 176 | going. |
| 177 | |
| 178 | <p> |
| 179 | Two heavily recurring metacharacters are: |
| 180 | <p> |
| 181 | <table width=100% border=0> |
| 182 | <tr> |
| 183 | <td bgcolor="#f0f0f0"> |
| 184 | <code>*</code> and <code>+</code> |
| 185 | </td> |
| 186 | </tr> |
| 187 | </table> |
| 188 | |
| 189 | <p> |
| 190 | They are called quantifiers and tell the engine to look for several occurrences |
| 191 | of a character; the quantifier always precedes the character at hand. The |
| 192 | <code><b>*</b></code> character matches <b>zero or more</b> occurrences of the |
| 193 | character in a row, the <code><b>+</b></code> character is similar but matches |
| 194 | <b><i>one</i> or more</b>. |
| 195 | |
| 196 | <p> |
| 197 | So if you decided to find words which had the character <code><b>c</b></code> |
| 198 | in it you might be tempted to write: |
| 199 | <p> |
| 200 | <table width=100% border=0> |
| 201 | <tr> |
| 202 | <td bgcolor="#f0f0f0"> |
| 203 | <code>c*</code> |
| 204 | </td> |
| 205 | </tr> |
| 206 | </table> |
| 207 | |
| 208 | <p> |
| 209 | What might come as a surprise to you is that you will find an enormous amount |
| 210 | of matches, even words with no <code><b>c</b></code> in them will match. |
| 211 | "How is this so?" you ask. Well, the answer is simple. Recall that the |
| 212 | <code><b>*</b></code> character matches <b>zero</b> or more characters, and |
| 213 | that's exactly what you matched, zero characters. |
| 214 | |
| 215 | <p> |
| 216 | You see, in <i>regular expressions</i> you have the possibility to match what |
| 217 | is called the <b>empty string</b>, which is simply a string with zero size. |
| 218 | This empty string can actually be found in all texts. For instance the word: |
| 219 | <p> |
| 220 | <table width=100% border=0> |
| 221 | <tr> |
| 222 | <td bgcolor="#f0f0f0"> |
| 223 | <code>go</code> |
| 224 | </td> |
| 225 | </tr> |
| 226 | </table> |
| 227 | <p> |
| 228 | contains three empty strings. They are contained at the position right before |
| 229 | the <code><b>g</b></code>, in between the <code><b>g</b></code> and the |
| 230 | <code><b>o</b></code> and after the <code><b>o</b></code>. And an empty string |
| 231 | contains exactly <b>one</b> empty string. At first this might seem like a |
| 232 | really silly thing to do, but you'll learn later on how this is used in more |
| 233 | complex expressions. |
| 234 | |
| 235 | <p> |
| 236 | So with this knowledge we might want to change our expression to: |
| 237 | <p> |
| 238 | <table width=100% border=0> |
| 239 | <tr> |
| 240 | <td bgcolor="#f0f0f0"> |
| 241 | <code>c+</code> |
| 242 | </td> |
| 243 | </tr> |
| 244 | </table> |
| 245 | <p> |
| 246 | and voila we get only words with <code><b>c</b></code> in them. |
| 247 | |
| 248 | <p> |
| 249 | The next metacharacter you'll learn is: |
| 250 | <p> |
| 251 | <table width=100% border=0> |
| 252 | <tr> |
| 253 | <td bgcolor="#f0f0f0"> |
| 254 | <code>?</code> |
| 255 | </td> |
| 256 | </tr> |
| 257 | </table> |
| 258 | <p> |
| 259 | This simply tells the engine to either match the character or not (zero or |
| 260 | one). For instance the expression: |
| 261 | <p> |
| 262 | <table width=100% border=0> |
| 263 | <tr> |
| 264 | <td bgcolor="#f0f0f0"> |
| 265 | <code>cows?</code> |
| 266 | </td> |
| 267 | </tr> |
| 268 | </table> |
| 269 | <p> |
| 270 | will match any of these lines: |
| 271 | <p> |
| 272 | <table width=100% border=0> |
| 273 | <tr> |
| 274 | <td bgcolor="#f0f0f0"> |
| 275 | <code>cow</code><br> |
| 276 | <code>cows</code> |
| 277 | </td> |
| 278 | </tr> |
| 279 | </table> |
| 280 | <p> |
| 281 | These three metacharacters are simply a specialized scenario for the more |
| 282 | generalized quantifier: |
| 283 | <p> |
| 284 | <table width=100% border=0> |
| 285 | <tr> |
| 286 | <td bgcolor="#f0f0f0"> |
| 287 | <code>{n,m}</code> |
| 288 | </td> |
| 289 | </tr> |
| 290 | </table> |
| 291 | <p> |
| 292 | the <code><b>n</b></code> and <code><b>m</b></code> are respectively the |
| 293 | minimum and maximum size for the quantifier. For instance: |
| 294 | <p> |
| 295 | <table width=100% border=0> |
| 296 | <tr> |
| 297 | <td bgcolor="#f0f0f0"> |
| 298 | <code>{1,5}</code> |
| 299 | </td> |
| 300 | </tr> |
| 301 | </table> |
| 302 | <p> |
| 303 | means match one or up to five characters. You can also skip |
| 304 | <code><b>m</b></code> to allow for infinite match: |
| 305 | <p> |
| 306 | <table width=100% border=0> |
| 307 | <tr> |
| 308 | <td bgcolor="#f0f0f0"> |
| 309 | <code>{1,}</code> |
| 310 | </td> |
| 311 | </tr> |
| 312 | </table> |
| 313 | <p> |
| 314 | which matches one or more characters. This is exactly what the |
| 315 | <code><b>+</b></code> character does. So now you see the connection, |
| 316 | <code><b>*</b></code> is equal to <code><b>{0,}</b></code>, |
| 317 | <code><b>+</b></code> is equal to <code><b>{1,}</b></code> and |
| 318 | <code><b>?</b></code> is equal to <code><b>{0,1}</b></code>. |
| 319 | |
| 320 | <p> |
| 321 | The last thing you can do with the quantifier is to also skip the comma: |
| 322 | <p> |
| 323 | <table width=100% border=0> |
| 324 | <tr> |
| 325 | <td bgcolor="#f0f0f0"> |
| 326 | <code>{5}</code> |
| 327 | </td> |
| 328 | </tr> |
| 329 | </table> |
| 330 | <p> |
| 331 | which means to match 5 characters, no more, no less. |
| 332 | |
| 333 | <p> |
| 334 | <h2>Assertions</h2> |
| 335 | The next type of metacharacters is assertions. These will match if a given |
| 336 | assertion is true. The first pair of assertions are: |
| 337 | <p> |
| 338 | <table width=100% border=0> |
| 339 | <tr> |
| 340 | <td bgcolor="#f0f0f0"> |
| 341 | <code>^</code> and <code>$</code> |
| 342 | </td> |
| 343 | </tr> |
| 344 | </table> |
| 345 | <p> |
| 346 | which match the beginning of the line, and the end of the line, respectively. |
| 347 | Note that some <i>regular expression</i> implementations allows you to change |
| 348 | their behavior so that they will instead match the beginning of the text and |
| 349 | the end of the text. These assertions always match a zero length string, or in |
| 350 | other words, they match a position. For instance, if you wrote this expression: |
| 351 | <p> |
| 352 | <table width=100% border=0> |
| 353 | <tr> |
| 354 | <td bgcolor="#f0f0f0"> |
| 355 | <code>^The</code> |
| 356 | </td> |
| 357 | </tr> |
| 358 | </table> |
| 359 | <p> |
| 360 | it would match any line which began with the word <code><b>The</b></code>. |
| 361 | |
| 362 | <p> |
| 363 | The next assertion characters match at the beginning and end of a word; they |
| 364 | are: |
| 365 | <p> |
| 366 | <table width=100% border=0> |
| 367 | <tr> |
| 368 | <td bgcolor="#f0f0f0"> |
| 369 | <code><</code> and <code>></code> |
| 370 | </td> |
| 371 | </tr> |
| 372 | </table> |
| 373 | <p> |
| 374 | they come in handy when you want to match a word precisely. For instance: |
| 375 | <p> |
| 376 | <table width=100% border=0> |
| 377 | <tr> |
| 378 | <td bgcolor="#f0f0f0"> |
| 379 | <code>cow</code> |
| 380 | </td> |
| 381 | </tr> |
| 382 | </table> |
| 383 | <p> |
| 384 | would match any of the following words: |
| 385 | <p> |
| 386 | <table width=100% border=0> |
| 387 | <tr> |
| 388 | <td bgcolor="#f0f0f0"> |
| 389 | <code>cow</code><br> |
| 390 | <code>coward</code><br> |
| 391 | <code>cowage</code><br> |
| 392 | <code>cowboy</code><br> |
| 393 | <code>cowl</code> |
| 394 | </td> |
| 395 | </tr> |
| 396 | </table> |
| 397 | <p> |
| 398 | A small change to the expression: |
| 399 | <p> |
| 400 | <table width=100% border=0> |
| 401 | <tr> |
| 402 | <td bgcolor="#f0f0f0"> |
| 403 | <code><cow></code> |
| 404 | </td> |
| 405 | </tr> |
| 406 | </table> |
| 407 | <p> |
| 408 | and you'll only match the word <code><b>cow</b></code> in the text. |
| 409 | |
| 410 | <p> |
| 411 | One last thing to be said is that all literal characters are in fact assertions |
| 412 | themselves. The difference between them and the ones above is that literal |
| 413 | characters have a size. So for cleanliness sake we only use the word |
| 414 | "assertions" for those that are zero-width. |
| 415 | |
| 416 | <p> |
| 417 | <h2>Groups and Alternation</h2> |
| 418 | One thing you might have noticed when we explained quantifiers is that they |
| 419 | only worked on the character to the left. Since this pretty much limits our |
| 420 | expressions I'll explain other uses for quantifiers. Quantifiers can also be |
| 421 | used on metacharacters; using them on assertions is silly since assertions are |
| 422 | zero-width and matching one, two, three or more of them doesn't do any good. |
| 423 | However the grouping and sequence metacharacters are perfect for being |
| 424 | quantified. Let's first start with grouping. |
| 425 | |
| 426 | <p> |
| 427 | You can form groups, or subexpressions as they are frequently called, by using |
| 428 | the begin and end parenthesis characters: |
| 429 | <p> |
| 430 | <table width=100% border=0> |
| 431 | <tr> |
| 432 | <td bgcolor="#f0f0f0"> |
| 433 | <code>(</code> and <code>)</code> |
| 434 | </td> |
| 435 | </tr> |
| 436 | </table> |
| 437 | |
| 438 | <p> |
| 439 | The <code><b>(</b></code> starts the subexpression and the <code><b>)</b></code> |
| 440 | ends it. It is also possible to have one or more subexpressions inside a |
| 441 | subexpressions. The subexpression will match if the contents match. So mixing |
| 442 | this with quantifiers and assertions you can do: |
| 443 | <p> |
| 444 | <table width=100% border=0> |
| 445 | <tr> |
| 446 | <td bgcolor="#f0f0f0"> |
| 447 | <code>( ?ho)+</code> |
| 448 | </td> |
| 449 | </tr> |
| 450 | </table> |
| 451 | <p> |
| 452 | which matches all of the following lines: |
| 453 | <p> |
| 454 | <table width=100% border=0> |
| 455 | <tr> |
| 456 | <td bgcolor="#f0f0f0"> |
| 457 | <code>ho</code><br> |
| 458 | <code>ho ho</code><br> |
| 459 | <code>ho ho ho</code><br> |
| 460 | <code>hohoho</code> |
| 461 | </td> |
| 462 | </tr> |
| 463 | </table> |
| 464 | |
| 465 | <p> |
| 466 | Another use for subexpressions are to extract a portion of the match if it |
| 467 | matches. This is often used in conjunction with sequences, which are discussed |
| 468 | later. |
| 469 | |
| 470 | <p> |
| 471 | You can also use the result of a subexpression for what is called a back |
| 472 | reference. A back reference is given by using a backslashified digit, a single |
| 473 | non-zero digit. This leaves you with nine back references (0 through 9). The |
| 474 | back reference matches whatever the corresponding subexpression actually |
| 475 | matched (except that <code>{<i>article_contents_1</i>}</code> matches a null |
| 476 | character). To find the number of the subexpression, count the open parentheses |
| 477 | from the left. |
| 478 | |
| 479 | <p> |
| 480 | The uses for back references are somewhat limited, especially since you only |
| 481 | have nine of them, but on some rare occasion you might need it. Note some |
| 482 | <i>regular expression</i> implementations can use multi-digit numbers as long |
| 483 | as they don't start with a 0. |
| 484 | |
| 485 | <p> |
| 486 | Next are alternations, which allow you to match on any of many words. The |
| 487 | alternation character is: |
| 488 | <p> |
| 489 | <table width=100% border=0> |
| 490 | <tr> |
| 491 | <td bgcolor="#f0f0f0"> |
| 492 | <code>|</code> |
| 493 | </td> |
| 494 | </tr> |
| 495 | </table> |
| 496 | <p> |
| 497 | A sample usage is: |
| 498 | <p> |
| 499 | <table width=100% border=0> |
| 500 | <tr> |
| 501 | <td bgcolor="#f0f0f0"> |
| 502 | <code>Bill|Linus|Steve|Larry</code> |
| 503 | </td> |
| 504 | </tr> |
| 505 | </table> |
| 506 | <p> |
| 507 | would match either Bill, Linus, Steve or Larry. Mixing this with subexpressions |
| 508 | and quantifiers we can do: |
| 509 | <p> |
| 510 | <table width=100% border=0> |
| 511 | <tr> |
| 512 | <td bgcolor="#f0f0f0"> |
| 513 | <code>cow(ard|age|boy|l)?</code> |
| 514 | </td> |
| 515 | </tr> |
| 516 | </table> |
| 517 | <p> |
| 518 | which matches any of the following words but no others: |
| 519 | <p> |
| 520 | <table width=100% border=0> |
| 521 | <tr> |
| 522 | <td bgcolor="#f0f0f0"> |
| 523 | <code>cow</code><br> |
| 524 | <code>coward</code><br> |
| 525 | <code>cowage</code><br> |
| 526 | <code>cowboy</code><br> |
| 527 | <code>cowl</code> |
| 528 | </td> |
| 529 | </tr> |
| 530 | </table> |
| 531 | |
| 532 | <p> |
| 533 | I mentioned earlier in the article that not all of the expression must match |
| 534 | for the match to be successful. This can happen when you're using |
| 535 | subexpressions together with alternations. For example: |
| 536 | <p> |
| 537 | <table width=100% border=0> |
| 538 | <tr> |
| 539 | <td bgcolor="#f0f0f0"> |
| 540 | <code>((Donald|Dolly) Duck)|(Scrooge McDuck)</code> |
| 541 | </td> |
| 542 | </tr> |
| 543 | </table> |
| 544 | <p> |
| 545 | As you see only the left or right top subexpression will match, not both. This |
| 546 | is sometimes handy when you want to run a complex pattern in one subexpression |
| 547 | and if it fails try another one. |
| 548 | |
| 549 | <p> |
| 550 | <h2>Sequences</h2> |
| 551 | Last we have sequences, which define sequences of characters which can match. |
| 552 | Sometimes you don't want to match a word directly but rather something that |
| 553 | resembles one. The sequence characters are: |
| 554 | <p> |
| 555 | <table width=100% border=0> |
| 556 | <tr> |
| 557 | <td bgcolor="#f0f0f0"> |
| 558 | <code>[</code> and <code>]</code> |
| 559 | </td> |
| 560 | </tr> |
| 561 | </table> |
| 562 | |
| 563 | <p> |
| 564 | Any characters put inside the sequence brackets are treated as literal |
| 565 | characters, even metacharacters. The only special characters are |
| 566 | <code><b>-</b></code> which denotes character ranges, and <code><b>^</b></code> |
| 567 | which is used to negate a sequence. The sequence is somewhat similar to |
| 568 | alternation; the similarity is that only one of the items listed will match. |
| 569 | For instance: |
| 570 | <p> |
| 571 | <table width=100% border=0> |
| 572 | <tr> |
| 573 | <td bgcolor="#f0f0f0"> |
| 574 | <code>[a-z]</code> |
| 575 | </td> |
| 576 | </tr> |
| 577 | </table> |
| 578 | <p> |
| 579 | will match any lowercase characters which are in the English alphabet (a to z). |
| 580 | Another common sequence is: |
| 581 | <p> |
| 582 | <table width=100% border=0> |
| 583 | <tr> |
| 584 | <td bgcolor="#f0f0f0"> |
| 585 | <code>[a-zA-Z0-9]</code> |
| 586 | </td> |
| 587 | </tr> |
| 588 | </table> |
| 589 | |
| 590 | <p> |
| 591 | Which matches any lowercase or capital characters in the English alphabet as |
| 592 | well as numbers. Sequences are also mixed with quantifiers and assertions to |
| 593 | produce more elaborate searches. Example: |
| 594 | <p> |
| 595 | <table width=100% border=0> |
| 596 | <tr> |
| 597 | <td bgcolor="#f0f0f0"> |
| 598 | <code><[a-zA-Z]+></code> |
| 599 | </td> |
| 600 | </tr> |
| 601 | </table> |
| 602 | <p> |
| 603 | matches all whole words. This will match: |
| 604 | <p> |
| 605 | <table width=100% border=0> |
| 606 | <tr> |
| 607 | <td bgcolor="#f0f0f0"> |
| 608 | <code>cow</code><br> |
| 609 | <code>Linus</code><br> |
| 610 | <code>regular</code><br> |
| 611 | <code>expression</code> |
| 612 | </td> |
| 613 | </tr> |
| 614 | </table> |
| 615 | <p> |
| 616 | but will not match: |
| 617 | <p> |
| 618 | <table width=100% border=0> |
| 619 | <tr> |
| 620 | <td bgcolor="#f0f0f0"> |
| 621 | <code>200</code><br> |
| 622 | <code>x-files</code><br> |
| 623 | <code>C++</code> |
| 624 | </td> |
| 625 | </tr> |
| 626 | </table> |
| 627 | |
| 628 | <p> |
| 629 | Now if you wanted to find anything <i>but</i> words, the expression: |
| 630 | <p> |
| 631 | <table width=100% border=0> |
| 632 | <tr> |
| 633 | <td bgcolor="#f0f0f0"> |
| 634 | <code>[^a-zA-Z0-9]+</code> |
| 635 | </td> |
| 636 | </tr> |
| 637 | </table> |
| 638 | <p> |
| 639 | would find any sequences of characters which do not contain the English |
| 640 | alphabet or any numbers. |
| 641 | |
| 642 | <p> |
| 643 | Some implementations of <i>regular expressions</i> allow you to use shorthand |
| 644 | versions for commonly used sequences, they are: |
| 645 | <p> |
| 646 | <table width=100% border=0> |
| 647 | <tr> |
| 648 | <td bgcolor="#f0f0f0"> |
| 649 | <code>\d</code>, a digit (<code>[0-9]</code>)<br> |
| 650 | <code>\D</code>, a non-digit (<code>[^0-9]</code>)<br> |
| 651 | <code>\w</code>, a word (alphanumeric) (<code>[a-zA-Z0-9]</code>)<br> |
| 652 | <code>\W</code>, a non-word (<code>[^a-zA-Z0-9]</code>)<br> |
| 653 | <code>\s</code>, a whitespace (<code>[ \t\n\r\f]</code>)<br> |
| 654 | <code>\S</code>, a non-whitespace (<code>[^ \t\n\r\f]</code>) |
| 655 | </td> |
| 656 | </tr> |
| 657 | </table> |
| 658 | |
| 659 | <p> |
| 660 | <b>Wildcards</b><br> |
| 661 | For people who have some knowledge with wildcards (also known as |
| 662 | <i>file globs</i> or <i>file globbing</i>), I'll give a brief explanation on how |
| 663 | to convert them to <i>regular expressions</i>. After reading this article, you |
| 664 | probably have seen the similarities with wildcards. For instance: |
| 665 | <p> |
| 666 | <table width=100% border=0> |
| 667 | <tr> |
| 668 | <td bgcolor="#f0f0f0"> |
| 669 | <code>*.jpg</code> |
| 670 | </td> |
| 671 | </tr> |
| 672 | </table> |
| 673 | <p> |
| 674 | matches any text which end with <code><b>.jpg</b></code>. You can also specify |
| 675 | brackets with characters, as in: |
| 676 | <p> |
| 677 | <table width=100% border=0> |
| 678 | <tr> |
| 679 | <td bgcolor="#f0f0f0"> |
| 680 | <code>*.[ch]pp</code> |
| 681 | </td> |
| 682 | </tr> |
| 683 | </table> |
| 684 | <p> |
| 685 | matches any text which ends in <code><b>.cpp</b></code> or |
| 686 | <code><b>.hpp</b></code>. Altogether very similar to regular expressions. |
| 687 | |
| 688 | <p> |
| 689 | The <code><b>*</b></code> means match zero or more of anything in wildcards. As |
| 690 | we learned, we do this is regular expression with the punctuation mark and the |
| 691 | <code><b>*</b></code> quantifier. This gives: |
| 692 | <p> |
| 693 | <table width=100% border=0> |
| 694 | <tr> |
| 695 | <td bgcolor="#f0f0f0"> |
| 696 | <code>.*</code> |
| 697 | </td> |
| 698 | </tr> |
| 699 | </table> |
| 700 | <p> |
| 701 | Also remember to convert any punctuation marks from wildcards to be |
| 702 | backslashified. |
| 703 | |
| 704 | <p> |
| 705 | The <code><b>?</b></code> means match any character but do match |
| 706 | <b>something</b>. This is exactly what the punctuation mark does. |
| 707 | |
| 708 | <p> |
| 709 | Square brackets can be used untouched since they have the same meaning going |
| 710 | from wildcards to regular expressions. |
| 711 | |
| 712 | <p> |
| 713 | These leaves us with: |
| 714 | <ul> |
| 715 | <li>Replace any <code><b>*</b></code> characters with <code><b>.*</b></code> |
| 716 | <li>Replace any <code><b>?</b></code> characters with <code><b>.</b></code> |
| 717 | <li>Leave square brackets as they are. |
| 718 | <li>Replace any characters which are metacharacters with a backslashified |
| 719 | version. |
| 720 | </ul> |
| 721 | |
| 722 | <p> |
| 723 | <b>Examples</b><br> |
| 724 | <p> |
| 725 | <table width=100% border="0"> |
| 726 | <tr> |
| 727 | <td bgcolor="#f0f0f0"> |
| 728 | <code>*.jpg</code> |
| 729 | </td> |
| 730 | </tr> |
| 731 | </table> |
| 732 | <p> |
| 733 | would be converted to: |
| 734 | <p> |
| 735 | <table width=100% border=0> |
| 736 | <tr> |
| 737 | <td bgcolor="#f0f0f0"> |
| 738 | <code>.*\.jpg</code> |
| 739 | </td> |
| 740 | </tr> |
| 741 | </table> |
| 742 | |
| 743 | <p> |
| 744 | <table width=100% border=0> |
| 745 | <tr> |
| 746 | <td bgcolor="#f0f0f0"> |
| 747 | <code>ez*.[ch]pp</code> |
| 748 | </td> |
| 749 | </tr> |
| 750 | </table> |
| 751 | <p> |
| 752 | would convert to: |
| 753 | <p> |
| 754 | <table width=100% border=0> |
| 755 | <tr> |
| 756 | <td bgcolor="#f0f0f0"> |
| 757 | <code>ez.*\.[ch]pp</code> |
| 758 | </td> |
| 759 | </tr> |
| 760 | </table> |
| 761 | <p> |
| 762 | or alternatively: |
| 763 | <p> |
| 764 | <table width=100% border=0> |
| 765 | <tr> |
| 766 | <td bgcolor="#f0f0f0"> |
| 767 | <code>ez.*\.(cpp|hpp)</code> |
| 768 | </td> |
| 769 | </tr> |
| 770 | </table> |
| 771 | |
| 772 | <p> |
| 773 | <h3>Example Regular Expressions</h3> |
| 774 | To really get to know <i>regular expressions</i> I've left some commonly used |
| 775 | expressions on this page. Study them, experiment and try to understand exactly |
| 776 | what they are doing. |
| 777 | |
| 778 | <p> |
| 779 | Email validity: will only match email addresses which are valid, such as |
| 780 | "[email protected]": |
| 781 | <p> |
| 782 | <table width=100% border=0> |
| 783 | <tr> |
| 784 | <td bgcolor="#f0f0f0"> |
| 785 | <code>[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)+</code> |
| 786 | </td> |
| 787 | </tr> |
| 788 | </table> |
| 789 | |
| 790 | <p> |
| 791 | Email validity #2: matches email addresses with a name in front, like "John |
| 792 | Doe <[email protected]>": |
| 793 | <p> |
| 794 | <table width=100% border=0> |
| 795 | <tr> |
| 796 | <td bgcolor="#f0f0f0"> |
| 797 | <code>("?[a-zA-Z]+"?[ \t]*)+\<[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)+\></code> |
| 798 | </td> |
| 799 | </tr> |
| 800 | </table> |
| 801 | |
| 802 | <p> |
| 803 | Protocol validity: matches web based protocols such as "htpp://", |
| 804 | "ftp://" or "https://": |
| 805 | <p> |
| 806 | <table width=100% border=0> |
| 807 | <tr> |
| 808 | <td bgcolor="#f0f0f0"> |
| 809 | <code>[a-z]+://</code> |
| 810 | </td> |
| 811 | </tr> |
| 812 | </table> |
| 813 | |
| 814 | <p> |
| 815 | C/C++ includes: matches valid include statements in C/C++ files: |
| 816 | <p> |
| 817 | <table width=100% border=0> |
| 818 | <tr> |
| 819 | <td bgcolor="#f0f0f0"> |
| 820 | <code>^#include[ \t]+[<"][^>"]+[">]</code> |
| 821 | </td> |
| 822 | </tr> |
| 823 | </table> |
| 824 | |
| 825 | <p> |
| 826 | C++ end of line comments: |
| 827 | <p> |
| 828 | <table width=100% border=0> |
| 829 | <tr> |
| 830 | <td bgcolor="#f0f0f0"> |
| 831 | <code>//.+$</code> |
| 832 | </td> |
| 833 | </tr> |
| 834 | </table> |
| 835 | |
| 836 | <p> |
| 837 | C/C++ span line comments (it has one flaw, can you spot it?): |
| 838 | <p> |
| 839 | <table width=100% border=0> |
| 840 | <tr> |
| 841 | <td bgcolor="#f0f0f0"> |
| 842 | <code>/\*[^*]*\*/</code> |
| 843 | </td> |
| 844 | </tr> |
| 845 | </table> |
| 846 | |
| 847 | <p> |
| 848 | Floating point numbers: matches simple floating point numbers of the kind 1.2 |
| 849 | and 0.5: |
| 850 | <p> |
| 851 | <table width=100% border=0> |
| 852 | <tr> |
| 853 | <td bgcolor="#f0f0f0"> |
| 854 | <code>-?[0-9]+\.[0-9]+</code> |
| 855 | </td> |
| 856 | </tr> |
| 857 | </table> |
| 858 | |
| 859 | <p> |
| 860 | Hexadecimal numbers: matches C/C++ style hex numbers, <i>e.g.</i> |
| 861 | <code>0xcafebabe</code>: |
| 862 | <p> |
| 863 | <table width=100% border=0> |
| 864 | <tr> |
| 865 | <td bgcolor="#f0f0f0"> |
| 866 | <code>0x[0-9a-fA-F]+</code> |
| 867 | </td> |
| 868 | </tr> |
| 869 | </table> |
| 870 | |
| 871 | </body> |
| 872 | </html> |