<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Artisan Numérique</title>
  <link rel="alternate" type="text/html" href="http://artisan.karma-lab.net/node/1288"/>
  <link rel="self" type="application/atom+xml" href="http://artisan.karma-lab.net/node/1288/atom/feed"/>
  <id>http://artisan.karma-lab.net/node/1288/atom/feed</id>
  <updated>2008-02-12T11:16:42+01:00</updated>
  <entry>
    <title>Bloc-note &#039;Expressions Régulières&#039;</title>
    <link rel="alternate" type="text/html" href="http://artisan.karma-lab.net/node/1288" />
    <id>http://artisan.karma-lab.net/node/1288</id>
    <published>2007-12-06T00:38:52+01:00</published>
    <updated>2008-02-12T11:16:42+01:00</updated>
    <author>
      <name>Ulhume</name>
    </author>
    <category term="Perl" />
    <category term="Aucun" />
    <category term="Note" />
    <category term="OK" />
    <summary type="html"><![CDATA[<p>
  Vu que ça me fatigue de rechercher à chaque fois les mêmes regex's, ouverture d'un petit bloc-note dédié à l'art (non maîtrisé) du parsing. 
</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>
  Vu que ça me fatigue de rechercher à chaque fois les mêmes regex's, ouverture d'un petit bloc-note dédié à l'art (non maîtrisé) du parsing. 
</p>
<!--break-->

	<a name='chapter_1'></a>
  <h2>URL</h2>
	
<p>
  Pour parser une URL d'un coup, celle-là marche plutôt bien à un détail près, il englobe la query et le hash (ce qu'il y a après un éventuel #). Pour éviter cela, si cela pose problème, il faut enlever le dernier ? et systématiquement ajouter un # à la fin de l'url avant parsing, s'il n'y est pas déjà évidemment. : 

  <div class='code-block code-block-fragment'>
  <div class='container'>
  <span class="kw2">sub</span> Parse <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <a target="blank" href="http://perldoc.perl.org/functions/shift.html"><span class="kw3">shift</span></a> <span class="sy0">=~</span> <span class="co2">/^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$/g</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">my</span> <span class="br0">&#40;</span><span class="re0">$query</span><span class="sy0">,</span><span class="re0">$hash</span><span class="br0">&#41;</span> <span class="sy0">=</span> <a target="blank" href="http://perldoc.perl.org/functions/split.html"><span class="kw3">split</span></a><span class="br0">&#40;</span><span class="co2">/#/</span><span class="sy0">,</span><span class="co3">$7</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><a target="blank" href="http://perldoc.perl.org/functions/defined.html"><span class="kw3">defined</span></a> <span class="re0">$hash</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$hash</span><span class="sy0">=</span><span class="st0">'';<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; if (!defined $query) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query='</span><span class="st0">';<br />
&nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query=substr($query,1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; my %result = (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protocol=&gt;$2,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; host=&gt;$3,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path=&gt;$4,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file=&gt;$6,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query=&gt;$query,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hash=&gt;$hash<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br />
&nbsp; &nbsp; &nbsp; &nbsp; return %result;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; return {};<br />
}<br />
</span
  </div>
  
  </div>
</p>    ]]></content>
  </entry>
</feed>
