home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / share / doc / HOWTO-regexps.htm < prev    next >
Encoding:
Extensible Markup Language  |  2004-05-19  |  28.4 KB  |  1,144 lines

  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xml:lang="en" lang="en">
  4. <head>
  5.   <title>zez.org: about code</title>
  6.   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  7. </head>
  8.  
  9. <body>
  10.  
  11. <table width="100%" cellspacing="0" cellpadding="0" border="0">
  12.   <tbody>
  13.     <tr>
  14.       <td align="center"><h3>Found at:
  15.         http://publish.ez.no/article/articleprint/11/</h3>
  16.       </td>
  17.     </tr>
  18.   </tbody>
  19. </table>
  20.  
  21. <table width="100%" cellspacing="0" cellpadding="0" border="0">
  22.   <tbody>
  23.     <tr>
  24.       <td><h1>Regular Expressions explained</h1>
  25.       </td>
  26.     </tr>
  27.   </tbody>
  28. </table>
  29. <hr noshade="noshade" size="4" />
  30. <br />
  31.  
  32.  
  33. <table width="100%" cellspacing="0" cellpadding="0" border="0">
  34.   <tbody>
  35.     <tr>
  36.       <td><p class="byline">Author: <a class="byline"
  37.         href="/article/author/view/26">Jan Borsodi</a></p>
  38.       </td>
  39.       <td align="right"><p class="byline">Publishing date: 30.10.2000
  40.         18:02</p>
  41.       </td>
  42.     </tr>
  43.   </tbody>
  44. </table>
  45.  
  46. <p>This article will give you an introduction to the world of <i>regular
  47. expressions</i>. I'll start off with explaining what regular expressions are
  48. and introduce it's syntax, then some examples with varying complexity and
  49. last a list of tools which use <i>regular expressions</i>.</p>
  50.  
  51. <p><span style="color: #FF0000">[Note: Puppy has a regular expression
  52. evaluation and learning tool in the Utilities menu]</span></p>
  53.  
  54. <h2>Concept</h2>
  55. A <i>regular expression</i> is a text pattern consisting of a combination of
  56. alphanumeric characters and special characters known as metacharacters. A
  57. close relative is in fact the <i>wildcard expression</i> which are often used
  58. in file management. The pattern is used to match against text strings. The
  59. result of a match is either successful or not, however when a match is
  60. successful not all of the pattern must match, this is explained later in the
  61. article.<br />
  62. <br />
  63. You'll find that <i>regular expressions</i> are used in three different ways:
  64. Regular text match, search and replace and splitting. The latter is basicly
  65. the same as the reverse match ie. everything the <i>regular expression</i>
  66. did not match.<br />
  67. <br />
  68. <i>Regular expressions</i> are often simply called regexps or RE, but for
  69. consistency I'll be referring to it with it's full name.<br />
  70. <br />
  71. Due to the versatility of the <i>regular expression</i> it is widely used in
  72. text processing and parsing. UNIX users are probably familiar with them
  73. trough the use of the programs, <i>grep</i>, <i>sed</i>, <i>awk</i> and
  74. <i>ed</i>. Text editors such as <i>(X)Emacs</i> and <i>vi</i> also use them
  75. heavily. Probably the most known use of  <i>regular expressions</i> are in
  76. the programming language Perl, you'll find that Perl sports the most advanced
  77. <i>regular expression</i> implementation to this day.<br />
  78.  
  79.  
  80. <h2>Usage</h2>
  81. Now you're probably wondering why you should bother to learn <i>regular
  82. expressions</i>. Well if you're a normal computer user your benefits from
  83. using them are somewhat small, however if you're either a developer or a
  84. system administrator you'll find that knowing <i>regular expressions</i> will
  85. make your (professional)life so much better.<br />
  86. <br />
  87. Developers can use them to parse text files, fix up code and other wonders.
  88. System administrators can use them to search trough logs, automate boring
  89. tasks and sniff the network traffic for unauthorized activity.<br />
  90. <br />
  91. Actually I would go so far as to say it's a crime for a System Administrator
  92. not to have <b>any</b> knowledge of <i>regular expressions</i>.<br />
  93.  
  94.  
  95. <h2>Quantifiers</h2>
  96. Before I start explaining the syntax you might want to jump to the last page
  97. to learn which programs you can use to test out the examples in this
  98. article.<br />
  99. <br />
  100. The contents of an expression is, as explained earlier, a combination of
  101. alphanumeric characters and metacharacters. An alphanumeric character is
  102. either a letter from the alphabet<br />
  103. <br clear="all" />
  104.  
  105.  
  106. <p></p>
  107.  
  108. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  109.   <tbody>
  110.     <tr>
  111.       <td bgcolor="#f0f0f0"><pre>abc</pre>
  112.       </td>
  113.     </tr>
  114.   </tbody>
  115. </table>
  116. or a number<br />
  117. <br clear="all" />
  118.  
  119.  
  120. <p></p>
  121.  
  122. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  123.   <tbody>
  124.     <tr>
  125.       <td bgcolor="#f0f0f0"><pre>123</pre>
  126.       </td>
  127.     </tr>
  128.   </tbody>
  129. </table>
  130. Actually in the world of regular expressions any character which is not a
  131. metacharacter will match itself(often called literal characters), however a
  132. lot of the time you're mostly concerned with the alphanumeric characters. A
  133. very special character is the backslash <b>\</b>, this turns any
  134. metacharacters into literal characters, and alphanumeric characters into a
  135. sort of metacharacter or sequence. The metacharacters are:<br />
  136. <br clear="all" />
  137.  
  138.  
  139. <p></p>
  140.  
  141. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  142.   <tbody>
  143.     <tr>
  144.       <td bgcolor="#f0f0f0"><pre>\ | ( ) [  {  ^ $ * + ? . < ></pre>
  145.       </td>
  146.     </tr>
  147.   </tbody>
  148. </table>
  149. With that said normal characters don't sound too interesting so lets jump to
  150. the our very first meta characters.<br />
  151. <br />
  152. The punctuation mark, or dot, <b>.</b> needs explaining first since it often
  153. leads to confusion. This character will not, as many might think, match the
  154. punctuation in a line, it is instead a special meta character which matches
  155. any character. Using this were you wanted to find the end of the line or the
  156. decimal in a floating number will lead to strange results. As explained
  157. above, you need to backslashify it to get the literal meaning. For instance
  158. take this expression<br />
  159. <br clear="all" />
  160.  
  161.  
  162. <p></p>
  163.  
  164. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  165.   <tbody>
  166.     <tr>
  167.       <td bgcolor="#f0f0f0"><pre>1.23</pre>
  168.       </td>
  169.     </tr>
  170.   </tbody>
  171. </table>
  172. will match the number 1.23 in a text as you might have guessed, but it will
  173. also match these next lines<br />
  174. <br clear="all" />
  175.  
  176.  
  177. <p></p>
  178.  
  179. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  180.   <tbody>
  181.     <tr>
  182.       <td bgcolor="#f0f0f0"><pre>1x23
  183. 1 23
  184. 1-23</pre>
  185.       </td>
  186.     </tr>
  187.   </tbody>
  188. </table>
  189. to make the expression <b>only</b> match the floating number we change it
  190. to<br />
  191. <br clear="all" />
  192.  
  193.  
  194. <p></p>
  195.  
  196. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  197.   <tbody>
  198.     <tr>
  199.       <td bgcolor="#f0f0f0"><pre>1\.23</pre>
  200.       </td>
  201.     </tr>
  202.   </tbody>
  203. </table>
  204. Remember this, it's very important. Now with that said we can get the show
  205. going.<br />
  206. <br />
  207. Two heavily recurring metacharacters are<br />
  208. <br clear="all" />
  209.  
  210.  
  211. <p></p>
  212.  
  213. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  214.   <tbody>
  215.     <tr>
  216.       <td bgcolor="#f0f0f0"><pre>* and +</pre>
  217.       </td>
  218.     </tr>
  219.   </tbody>
  220. </table>
  221. They are called quantifiers and tells the engine to look for several
  222. occurrences of a characters, the quantifier always precedes the character at
  223. hand. The <b>*</b> character matches zero or more occurrences of the
  224. character in a row, the <b>+</b> characters is similar but matches one or
  225. more.<br />
  226. <br />
  227. So what if you decided to find words which had the character <i>c</i> in it
  228. you might be tempted to write:<br />
  229. <br clear="all" />
  230.  
  231.  
  232. <p></p>
  233.  
  234. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  235.   <tbody>
  236.     <tr>
  237.       <td bgcolor="#f0f0f0"><pre>c*</pre>
  238.       </td>
  239.     </tr>
  240.   </tbody>
  241. </table>
  242. What might come as a surprise to you is that you will find an enormous amount
  243. of matches, even words with no c in it will match. How so you ask, well the
  244. answer is simple. Recall that the <b>*</b> character matches <b>zero</b> or
  245. more characters, well thats exactly what you did, zero characters.<br />
  246. You see in <i>regular expressions</i> you have the possibility to match what
  247. is called <b>the empty string</b>, which is simply a string with zero size.
  248. This empty string can actually be found in all texts, for instance the
  249. word:<br />
  250. <br clear="all" />
  251.  
  252.  
  253. <p></p>
  254.  
  255. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  256.   <tbody>
  257.     <tr>
  258.       <td bgcolor="#f0f0f0"><pre>go</pre>
  259.       </td>
  260.     </tr>
  261.   </tbody>
  262. </table>
  263. contains three empty strings. They are contained at the position right before
  264. the <b>g</b>, in between the <b>g</b> and the <b>o</b> and after the
  265. <b>o</b>. And an empty string contains exactly <b>one</b> empty string. At
  266. first this might seem like a really silly thing to do but you'll learn later
  267. on how this is used in more complex expressions.<br />
  268. <br />
  269. So with this knowledge we might want to change our expression to:<br />
  270. <br clear="all" />
  271.  
  272.  
  273. <p></p>
  274.  
  275. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  276.   <tbody>
  277.     <tr>
  278.       <td bgcolor="#f0f0f0"><pre>c+</pre>
  279.       </td>
  280.     </tr>
  281.   </tbody>
  282. </table>
  283. and voila we get only words with c in them.<br />
  284. <br />
  285. The next metacharacter you'll learn is:<br />
  286. <br clear="all" />
  287.  
  288.  
  289. <p></p>
  290.  
  291. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  292.   <tbody>
  293.     <tr>
  294.       <td bgcolor="#f0f0f0"><pre>?</pre>
  295.       </td>
  296.     </tr>
  297.   </tbody>
  298. </table>
  299. This simply tells the engine to either match the character or not (zero or
  300. one). For instance the expression:<br />
  301. <br clear="all" />
  302.  
  303.  
  304. <p></p>
  305.  
  306. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  307.   <tbody>
  308.     <tr>
  309.       <td bgcolor="#f0f0f0"><pre>cows?</pre>
  310.       </td>
  311.     </tr>
  312.   </tbody>
  313. </table>
  314. will match any of these lines:<br />
  315. <br clear="all" />
  316.  
  317.  
  318. <p></p>
  319.  
  320. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  321.   <tbody>
  322.     <tr>
  323.       <td bgcolor="#f0f0f0"><pre>cow
  324. cows</pre>
  325.       </td>
  326.     </tr>
  327.   </tbody>
  328. </table>
  329. <br />
  330. These three metacharacters are simply a specialized scenario for the more
  331. generalized quantifier<br />
  332. <br clear="all" />
  333.  
  334.  
  335. <p></p>
  336.  
  337. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  338.   <tbody>
  339.     <tr>
  340.       <td bgcolor="#f0f0f0"><pre>{n,m}</pre>
  341.       </td>
  342.     </tr>
  343.   </tbody>
  344. </table>
  345. the <b>n</b> and <b>m</b> are respectively the minimum and maximum size for
  346. the quantifier. For instance<br />
  347. <br clear="all" />
  348.  
  349.  
  350. <p></p>
  351.  
  352. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  353.   <tbody>
  354.     <tr>
  355.       <td bgcolor="#f0f0f0"><pre>{1,5}</pre>
  356.       </td>
  357.     </tr>
  358.   </tbody>
  359. </table>
  360. means match one or up to five characters. You can also skip m to allow for
  361. infinite match:<br />
  362. <br clear="all" />
  363.  
  364.  
  365. <p></p>
  366.  
  367. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  368.   <tbody>
  369.     <tr>
  370.       <td bgcolor="#f0f0f0"><pre>{1,}</pre>
  371.       </td>
  372.     </tr>
  373.   </tbody>
  374. </table>
  375. which matches one or more characters. This is exactly what the <b>+</b>
  376. characters does. So now you see the connection, <b>*</b> is equal to
  377. <b>{0,}</b>, <b>+</b> is equal to <b>{1,}</b> and <b>?</b> is equal to
  378. <b>{0,1}</b>.<br />
  379. The last thing you can do with the quantifier is to also skip the comma,<br />
  380. <br clear="all" />
  381.  
  382.  
  383. <p></p>
  384.  
  385. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  386.   <tbody>
  387.     <tr>
  388.       <td bgcolor="#f0f0f0"><pre>{5}</pre>
  389.       </td>
  390.     </tr>
  391.   </tbody>
  392. </table>
  393. which means to match 5 characters, no more no less.<br />
  394.  
  395.  
  396. <h2>Assertions</h2>
  397. The next type of metacharacters are assertions, these will match if a given
  398. assertion is true. The first pair of assertions are<br />
  399. <br clear="all" />
  400.  
  401.  
  402. <p></p>
  403.  
  404. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  405.   <tbody>
  406.     <tr>
  407.       <td bgcolor="#f0f0f0"><pre>^ and $</pre>
  408.       </td>
  409.     </tr>
  410.   </tbody>
  411. </table>
  412. which match the beginning of the line and the end of the line. Note that some
  413. <i>regular expression</i> implementations allows you to change their behavior
  414. so that they will instead match the beginning of the text and the end of the
  415. text. These assertions always match a zero length string, or in other words
  416. they match a position. For instance if you wrote this expression:<br />
  417. <br clear="all" />
  418.  
  419.  
  420. <p></p>
  421.  
  422. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  423.   <tbody>
  424.     <tr>
  425.       <td bgcolor="#f0f0f0"><pre>^The</pre>
  426.       </td>
  427.     </tr>
  428.   </tbody>
  429. </table>
  430. it would match any line which began with the word <b>The</b>.<br />
  431. <br />
  432. The next assertion characters match at the beginning and end of a word, they
  433. are:<br />
  434. <br clear="all" />
  435.  
  436.  
  437. <p></p>
  438.  
  439. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  440.   <tbody>
  441.     <tr>
  442.       <td bgcolor="#f0f0f0"><pre>< and ></pre>
  443.       </td>
  444.     </tr>
  445.   </tbody>
  446. </table>
  447. they come in handy when you want to match a word precisely, for instance:<br
  448. />
  449. <br clear="all" />
  450.  
  451.  
  452. <p></p>
  453.  
  454. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  455.   <tbody>
  456.     <tr>
  457.       <td bgcolor="#f0f0f0"><pre>cow</pre>
  458.       </td>
  459.     </tr>
  460.   </tbody>
  461. </table>
  462. would match any of the following words<br />
  463. <br clear="all" />
  464.  
  465.  
  466. <p></p>
  467.  
  468. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  469.   <tbody>
  470.     <tr>
  471.       <td bgcolor="#f0f0f0"><pre>cow
  472. coward
  473. cowage
  474. cowboy
  475. cowl</pre>
  476.       </td>
  477.     </tr>
  478.   </tbody>
  479. </table>
  480. a small change to the expression:<br />
  481. <br clear="all" />
  482.  
  483.  
  484. <p></p>
  485.  
  486. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  487.   <tbody>
  488.     <tr>
  489.       <td bgcolor="#f0f0f0"><pre><cow></pre>
  490.       </td>
  491.     </tr>
  492.   </tbody>
  493. </table>
  494. and you'll only match the word <b>cow</b> in the text.<br />
  495. <br />
  496. One last thing to be said is that all literal characters are in fact
  497. assertions themselves, the difference between them and the ones above is that
  498. literal ones has a size. So for cleanliness sake we only use the word
  499. assertions for those that are zero-width.<br />
  500.  
  501.  
  502. <h2>Groups and alternation</h2>
  503. One thing you might have noticed when we explained quantifiers is that they
  504. only worked on the character to the left, since this pretty much limits our
  505. expressions I'll explain other uses for quantifiers. Quantifiers can also be
  506. used on metacharacters, using them on assertions is silly since they are
  507. zero-width and matching one, two, three or more of them doesn't do any good.
  508. However the grouping and sequence metacharacters are perfect for being
  509. quantified. Let's first start with grouping.<br />
  510. <br />
  511. You can form groups, or subexpressions as they are frequently called, by
  512. using the begin and end parenthesis characters:<br />
  513. <br clear="all" />
  514.  
  515.  
  516. <p></p>
  517.  
  518. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  519.   <tbody>
  520.     <tr>
  521.       <td bgcolor="#f0f0f0"><pre>( and )</pre>
  522.       </td>
  523.     </tr>
  524.   </tbody>
  525. </table>
  526. The <b>(</b> starts the subexpression and the <b>)</b> ends it. It is also
  527. possible to have one or more subexpressions inside a subexpressions. The
  528. subexpression will match if the contents match. So mixing this with
  529. quantifiers and assertions you can do:<br />
  530. <br clear="all" />
  531.  
  532.  
  533. <p></p>
  534.  
  535. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  536.   <tbody>
  537.     <tr>
  538.       <td bgcolor="#f0f0f0"><pre>( ?ho)+</pre>
  539.       </td>
  540.     </tr>
  541.   </tbody>
  542. </table>
  543. which matches all of the following lines<br />
  544. <br clear="all" />
  545.  
  546.  
  547. <p></p>
  548.  
  549. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  550.   <tbody>
  551.     <tr>
  552.       <td bgcolor="#f0f0f0"><pre>ho
  553. ho ho
  554. ho ho ho
  555. hohoho</pre>
  556.       </td>
  557.     </tr>
  558.   </tbody>
  559. </table>
  560. Another use for the subexpressions are to extract a portion of the match if
  561. it matches, this is often used in conjunction with sequences which is
  562. discussed later.<br />
  563. <br />
  564. You can also use the result of a subexpression for what is called a back
  565. reference. A back reference is given by using a backslashified digit, only a
  566. single non-zero digit, this leaves you with nine back references.<br />
  567. The back reference matches whatever the corresponding subexpression actually
  568. matched (except that {article_contents_1} matches a null character). To find
  569. the number of the subexpression count the left parentheses from the left.<br
  570. />
  571. <br />
  572. The use for back references are somewhat limited, especially since you only
  573. have nine of them, but on some rare occasion you might need it. Note some
  574. <i>regular expression</i> implementations can use multi-digit numbers as long
  575. as they don't start with a 0.<br />
  576. <br />
  577. Next is alternations which allows you to match on of many words, the
  578. alternation character is<br />
  579. <br clear="all" />
  580.  
  581.  
  582. <p></p>
  583.  
  584. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  585.   <tbody>
  586.     <tr>
  587.       <td bgcolor="#f0f0f0"><pre>|</pre>
  588.       </td>
  589.     </tr>
  590.   </tbody>
  591. </table>
  592. a sample usage is:<br />
  593. <br clear="all" />
  594.  
  595.  
  596. <p></p>
  597.  
  598. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  599.   <tbody>
  600.     <tr>
  601.       <td bgcolor="#f0f0f0"><pre>Bill|Linus|Steve|Larry</pre>
  602.       </td>
  603.     </tr>
  604.   </tbody>
  605. </table>
  606. would match either Bill, Linus, Steve or Larry, and mixing this with
  607. subexpressions and quantifiers we can do:<br />
  608. <br clear="all" />
  609.  
  610.  
  611. <p></p>
  612.  
  613. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  614.   <tbody>
  615.     <tr>
  616.       <td bgcolor="#f0f0f0"><pre>cow(ard|age|boy|l)?</pre>
  617.       </td>
  618.     </tr>
  619.   </tbody>
  620. </table>
  621. which matches any of the following words but none other<br />
  622. <br clear="all" />
  623.  
  624.  
  625. <p></p>
  626.  
  627. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  628.   <tbody>
  629.     <tr>
  630.       <td bgcolor="#f0f0f0"><pre>cow
  631. coward
  632. cowage
  633. cowboy
  634. cowl</pre>
  635.       </td>
  636.     </tr>
  637.   </tbody>
  638. </table>
  639. I mentioned earlier in the article that not all of the expression must match
  640. for the match to be successful, this can happen when you're using
  641. subexpressions together with alternations. For instance<br />
  642. <br clear="all" />
  643.  
  644.  
  645. <p></p>
  646.  
  647. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  648.   <tbody>
  649.     <tr>
  650.       <td bgcolor="#f0f0f0"><pre>((Donald|Dolly) Duck)|(Scrooge McDuck)</pre>
  651.       </td>
  652.     </tr>
  653.   </tbody>
  654. </table>
  655. As you see only the left or right top subexpression will match, not both,
  656. this is sometimes handy when you want to run a complex pattern in one
  657. subexpression and if it fails try another one.<br />
  658.  
  659.  
  660. <h2>Sequences</h2>
  661. Last we have sequences which defines sequences of characters which can match,
  662. sometimes you don't want match a word directly but rather something that
  663. resembles one. The sequence characters are<br />
  664. <br clear="all" />
  665.  
  666.  
  667. <p></p>
  668.  
  669. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  670.   <tbody>
  671.     <tr>
  672.       <td bgcolor="#f0f0f0"><pre>[ and ]</pre>
  673.       </td>
  674.     </tr>
  675.   </tbody>
  676. </table>
  677. any characters put inside the sequence brackets are treated as a literal
  678. character, even metacharacters. The only special characters are the <b>-</b>
  679. which denotes character ranges and the <b>^</b> which is used to negate a
  680. sequence. The sequence is somewhat similar with alternation, the similarity
  681. is that only one of the items listed will match. For instance<br />
  682. <br clear="all" />
  683.  
  684.  
  685. <p></p>
  686.  
  687. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  688.   <tbody>
  689.     <tr>
  690.       <td bgcolor="#f0f0f0"><pre>[a-z]</pre>
  691.       </td>
  692.     </tr>
  693.   </tbody>
  694. </table>
  695. will match any small characters which are in the English alphabet (a to z).
  696. Another common sequence is<br />
  697. <br clear="all" />
  698.  
  699.  
  700. <p></p>
  701.  
  702. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  703.   <tbody>
  704.     <tr>
  705.       <td bgcolor="#f0f0f0"><pre>[a-zA-Z0-9]</pre>
  706.       </td>
  707.     </tr>
  708.   </tbody>
  709. </table>
  710. which matches any small or capital characters in the English alphabet as well
  711. as numbers. Sequences are also mixed with quantifiers and assertions to
  712. produce more elaborate searches. For instance<br />
  713. <br clear="all" />
  714.  
  715.  
  716. <p></p>
  717.  
  718. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  719.   <tbody>
  720.     <tr>
  721.       <td bgcolor="#f0f0f0"><pre><[a-zA-Z]+></pre>
  722.       </td>
  723.     </tr>
  724.   </tbody>
  725. </table>
  726. matches all whole words. This will match<br />
  727. <br clear="all" />
  728.  
  729.  
  730. <p></p>
  731.  
  732. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  733.   <tbody>
  734.     <tr>
  735.       <td bgcolor="#f0f0f0"><pre>cow
  736. Linus
  737. regular
  738. expression</pre>
  739.       </td>
  740.     </tr>
  741.   </tbody>
  742. </table>
  743. but will not match<br />
  744. <br clear="all" />
  745.  
  746.  
  747. <p></p>
  748.  
  749. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  750.   <tbody>
  751.     <tr>
  752.       <td bgcolor="#f0f0f0"><pre>200
  753. x-files
  754. C++</pre>
  755.       </td>
  756.     </tr>
  757.   </tbody>
  758. </table>
  759. Now what if you wanted to find anything but words, the expression<br />
  760. <br clear="all" />
  761.  
  762.  
  763. <p></p>
  764.  
  765. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  766.   <tbody>
  767.     <tr>
  768.       <td bgcolor="#f0f0f0"><pre>[^a-zA-Z0-9]+</pre>
  769.       </td>
  770.     </tr>
  771.   </tbody>
  772. </table>
  773. would find any sequences of characters which does not contain the English
  774. alphabet or any numbers.<br />
  775. <br />
  776. Some implementations of <i>regular expressions</i> allows you to use
  777. shorthand versions for commonly used sequences, they are:<br />
  778. <br clear="all" />
  779.  
  780.  
  781. <p></p>
  782.  
  783. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  784.   <tbody>
  785.     <tr>
  786.       <td bgcolor="#f0f0f0"><pre>\d, a digit [0-9]
  787. \D, a non-digit [^0-9]
  788. \w, a word (alphanumeric) [a-zA-Z0-9]
  789. \W, a non-word [^a-zA-Z0-9]
  790. \s, a whitespace [ \t\n\r\f]
  791. \S, a non-whitespace [^ \t\n\r\f]</pre>
  792.       </td>
  793.     </tr>
  794.   </tbody>
  795. </table>
  796.  
  797. <h2>Wildcards</h2>
  798. For people who has some knowledge with wildcards I'll give a brief
  799. explanation on how to convert them to <i>regular expressions</i>. After
  800. reading this article you probably have seen the similarities with wildcards.
  801. For instance<br />
  802. <br clear="all" />
  803.  
  804.  
  805. <p></p>
  806.  
  807. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  808.   <tbody>
  809.     <tr>
  810.       <td bgcolor="#f0f0f0"><pre>*.jpg</pre>
  811.       </td>
  812.     </tr>
  813.   </tbody>
  814. </table>
  815. matches any text which end with .jpg. You can also specify brackets with
  816. characters, for instance<br />
  817. <br clear="all" />
  818.  
  819.  
  820. <p></p>
  821.  
  822. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  823.   <tbody>
  824.     <tr>
  825.       <td bgcolor="#f0f0f0"><pre>*.[ch]pp</pre>
  826.       </td>
  827.     </tr>
  828.   </tbody>
  829. </table>
  830. matches any text which ends in .cpp or .hpp. Altogether very similar to
  831. regular expressions.<br />
  832. <br />
  833.  
  834.  
  835. <h2>Converting the * operator</h2>
  836. The * means match zero or more of anything in wildcards, as we learned we do
  837. this is regular expression with the punctuation mark and the * quantifier.
  838. This gives<br />
  839. <br clear="all" />
  840.  
  841.  
  842. <p></p>
  843.  
  844. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  845.   <tbody>
  846.     <tr>
  847.       <td bgcolor="#f0f0f0"><pre>.*</pre>
  848.       </td>
  849.     </tr>
  850.   </tbody>
  851. </table>
  852. Also remember to convert any punctuation marks from wildcards to be
  853. backslashified.<br />
  854. <br />
  855.  
  856.  
  857. <h2>Converting the ? operator</h2>
  858. The ? means match any character but do match <b>something</b>, this is
  859. exactly what the punctuation mark does.<br />
  860. <br />
  861.  
  862.  
  863. <h2>Converting the [] operator</h2>
  864. The square bracket can be used untouched since they have the same meaning
  865. going from wildcards to regular expressions.<br />
  866. <br />
  867. These leaves us with:<br />
  868. Replace any * characters with .*<br />
  869. Replace any ? characters with .<br />
  870. Leave square brackets as they are.<br />
  871. Replace any characters which are metacharacters with a backslashified
  872. variant.<br />
  873. <br />
  874.  
  875.  
  876. <h2>Examples</h2>
  877. <br clear="all" />
  878.  
  879.  
  880. <p></p>
  881.  
  882. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  883.   <tbody>
  884.     <tr>
  885.       <td bgcolor="#f0f0f0"><pre>*.jpg</pre>
  886.       </td>
  887.     </tr>
  888.   </tbody>
  889. </table>
  890. would be converted to<br />
  891. <br clear="all" />
  892.  
  893.  
  894. <p></p>
  895.  
  896. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  897.   <tbody>
  898.     <tr>
  899.       <td bgcolor="#f0f0f0"><pre>.*\.jpg</pre>
  900.       </td>
  901.     </tr>
  902.   </tbody>
  903. </table>
  904. <br />
  905. <br clear="all" />
  906.  
  907.  
  908. <p></p>
  909.  
  910. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  911.   <tbody>
  912.     <tr>
  913.       <td bgcolor="#f0f0f0"><pre>ez*.[ch]pp</pre>
  914.       </td>
  915.     </tr>
  916.   </tbody>
  917. </table>
  918. would be convert to<br />
  919. <br clear="all" />
  920.  
  921.  
  922. <p></p>
  923.  
  924. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  925.   <tbody>
  926.     <tr>
  927.       <td bgcolor="#f0f0f0"><pre>ez.*\.[ch]pp</pre>
  928.       </td>
  929.     </tr>
  930.   </tbody>
  931. </table>
  932. or alternatively<br />
  933. <br clear="all" />
  934.  
  935.  
  936. <p></p>
  937.  
  938. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  939.   <tbody>
  940.     <tr>
  941.       <td bgcolor="#f0f0f0"><pre>ez.*\.(cpp|hpp)</pre>
  942.       </td>
  943.     </tr>
  944.   </tbody>
  945. </table>
  946. <br />
  947.  
  948.  
  949. <h2>Examples</h2>
  950. To really get to know <i>regular expressions</i> I've left some commonly used
  951. expressions on this page. Study them, experiment and try to understand
  952. exactly what they are doing.<br />
  953. <br />
  954. Email validity, will only match email addresses which are valid, for instance
  955. user@host.com<br />
  956. <br clear="all" />
  957.  
  958.  
  959. <p></p>
  960.  
  961. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  962.   <tbody>
  963.     <tr>
  964.       <td bgcolor="#f0f0f0"><pre>[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)+</pre>
  965.       </td>
  966.     </tr>
  967.   </tbody>
  968. </table>
  969. <br />
  970. Email validity #2, matches email addresses with a name in front, for instance
  971. "Joe Doe <user@host.com>"<br />
  972. <br clear="all" />
  973.  
  974.  
  975. <p></p>
  976.  
  977. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  978.   <tbody>
  979.     <tr>
  980.       <td bgcolor="#f0f0f0"><pre>("?[a-zA-Z]+"?[ \t]*)+\<[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)+\></pre>
  981.       </td>
  982.     </tr>
  983.   </tbody>
  984. </table>
  985. <br />
  986. Protocol validity, matches web based protocols such as htpp://, ftp:// or
  987. https://<br />
  988. <br clear="all" />
  989.  
  990.  
  991. <p></p>
  992.  
  993. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  994.   <tbody>
  995.     <tr>
  996.       <td bgcolor="#f0f0f0"><pre>[a-z]+://</pre>
  997.       </td>
  998.     </tr>
  999.   </tbody>
  1000. </table>
  1001. <br />
  1002. C/C++ includes, matches valid include statements in C/C++ files.<br />
  1003. <br clear="all" />
  1004.  
  1005.  
  1006. <p></p>
  1007.  
  1008. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  1009.   <tbody>
  1010.     <tr>
  1011.       <td bgcolor="#f0f0f0"><pre>^#include[ \t]+[<"][^>"]+[">]</pre>
  1012.       </td>
  1013.     </tr>
  1014.   </tbody>
  1015. </table>
  1016. <br />
  1017. C++ end of line comments<br />
  1018. <br clear="all" />
  1019.  
  1020.  
  1021. <p></p>
  1022.  
  1023. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  1024.   <tbody>
  1025.     <tr>
  1026.       <td bgcolor="#f0f0f0"><pre>//.+$</pre>
  1027.       </td>
  1028.     </tr>
  1029.   </tbody>
  1030. </table>
  1031. <br />
  1032. C/C++ span line comments, it has one flaw, can you spot it?<br />
  1033. <br clear="all" />
  1034.  
  1035.  
  1036. <p></p>
  1037.  
  1038. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  1039.   <tbody>
  1040.     <tr>
  1041.       <td bgcolor="#f0f0f0"><pre>/\*[^*]*\*/</pre>
  1042.       </td>
  1043.     </tr>
  1044.   </tbody>
  1045. </table>
  1046. <br />
  1047. Floating point numbers, matches simple floating point numbers of the kind 1.2
  1048. and 0.5<br />
  1049. <br clear="all" />
  1050.  
  1051.  
  1052. <p></p>
  1053.  
  1054. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  1055.   <tbody>
  1056.     <tr>
  1057.       <td bgcolor="#f0f0f0"><pre>-?[0-9]+\.[0-9]+</pre>
  1058.       </td>
  1059.     </tr>
  1060.   </tbody>
  1061. </table>
  1062. <br />
  1063. Hexadecimal numbers, matches C/C++ style hex numbers, 0xcafebabe<br />
  1064. <br clear="all" />
  1065.  
  1066.  
  1067. <p></p>
  1068.  
  1069. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  1070.   <tbody>
  1071.     <tr>
  1072.       <td bgcolor="#f0f0f0"><pre>0x[0-9a-fA-F]+</pre>
  1073.       </td>
  1074.     </tr>
  1075.   </tbody>
  1076. </table>
  1077. <br />
  1078.  
  1079.  
  1080. <h2>Utilities</h2>
  1081. There exists several utilities which uses regular expression. I'll leave a
  1082. list of them with a short description:<br />
  1083. <br />
  1084.  
  1085.  
  1086. <h2>grep</h2>
  1087. Grep searches named input files for lines containing a match to the given
  1088. pattern. It can also be used to find files which contains a specific pattern,
  1089. for instance:<br />
  1090. <br clear="all" />
  1091.  
  1092.  
  1093. <p></p>
  1094.  
  1095. <table width="100%" cellspacing="0" cellpadding="4" border="0">
  1096.   <tbody>
  1097.     <tr>
  1098.       <td bgcolor="#f0f0f0"><pre>grep -E "cow|vache" * >/dev/null && echo "Found a cow"</pre>
  1099.       </td>
  1100.     </tr>
  1101.   </tbody>
  1102. </table>
  1103. <br />
  1104. This is utility is rather common on Linux distributions, but if you don't
  1105. have it you can grab a version on <a
  1106. href="http://www.gnu.org/gnulist/production/grep.html">the GNU page</a><br />
  1107. <br />
  1108. A small tip is to enable extended regular expressions with the options -E, if
  1109. not a lot of the metacharacters explained in this article won't work.<br />
  1110. <br />
  1111.  
  1112.  
  1113. <h2>sed</h2>
  1114. Sed is a stream editor. A stream editor is used to perform basic text
  1115. transformations on an input stream.<br />
  1116. <br />
  1117. This is utility is rather common on Linux distributions, but if you don't
  1118. have it you can grab a version on <a
  1119. href="http://www.gnu.org/gnulist/production/sed.html">the GNU page</a><br />
  1120. <br />
  1121.  
  1122.  
  1123. <h2>gawk</h2>
  1124. Gawk  is  the GNU Project's implementation of the AWK programming language.
  1125. It conforms to the definition of the language in  the POSIX  1003.2 Command
  1126. Language And Utilities Standard.<br />
  1127. <br />
  1128. This is utility is rather common on Linux distributions, but if you don't
  1129. have it you can grab a version on <a href="http://www.gnu.org/">the GNU
  1130. page</a><br />
  1131. <br />
  1132. <span style="color: #FF0000; background-color: #FFFFFF">[document edited
  1133. here]</span><br style="color: #FF0000; background-color: #FFFFFF" />
  1134. <i>Regular expression</i> related links:<br />
  1135. <br />
  1136. <a href="http://www.plover.com/~mjd/perl/NPC/index.html">Regular Expressions
  1137. and NP-Completeness</a><br />
  1138. <a href="http://www.cs.rochester.edu/u/leblanc/csc173/fa/re.html">Equivalence
  1139. of Regular Expressions and Finite Automata</a><br />
  1140. <a href="http://virtual.park.uga.edu/humcomp/perl/regex2a.html">Perl Regular
  1141. Expression Tutorial</a> <br clear="all" />
  1142. </body>
  1143. </html>
  1144.