You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

169 lines
10 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>YBlog - replace all except some part</title>
<meta name="keywords" content="ruby, regexp, regular expression" />
<link rel="shortcut icon" type="image/x-icon" href="../../../../Scratch/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/css/y.css" />
<link rel="stylesheet" type="text/css" href="/css/legacy.css" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="apple-touch-icon" href="../../../../Scratch/img/about/FlatAvatar@2x.png" />
<!--[if lt IE 9]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<![endif]-->
<!-- IndieAuth -->
<link href="https://twitter.com/yogsototh" rel="me">
<link href="https://github.com/yogsototh" rel="me">
<link href="mailto:yann.esposito@gmail.com" rel="me">
<link rel="pgpkey" href="../../../../pubkey.txt">
</head>
<body lang="en" class="article">
<div id="content">
<div id="header">
<div id="choix">
<span id="choixlang">
<a href="../../../../Scratch/fr/blog/2009-09-replace-all-except-some-part/">French</a>
</span>
<span class="tomenu"><a href="#navigation">↓ Menu ↓</a></span>
<span class="flush"></span>
</div>
</div>
<div id="titre">
<h1>replace all except some part</h1>
</div>
<div class="flush"></div>
<div id="afterheader" class="article">
<div class="corps">
<p>My problem is simple:</p>
<p>I want to filter a text except some part of it. I can match easily the part I don’t want to be filtered. For example</p>
<div>
<div class="sourceCode" id="cb1"><pre class="sourceCode html"><code class="sourceCode html"><a class="sourceLine" id="cb1-1" title="1">...</a>
<a class="sourceLine" id="cb1-2" title="2">text</a>
<a class="sourceLine" id="cb1-3" title="3">...</a>
<a class="sourceLine" id="cb1-4" title="4">BEGIN not to filter</a>
<a class="sourceLine" id="cb1-5" title="5">...</a>
<a class="sourceLine" id="cb1-6" title="6">text</a>
<a class="sourceLine" id="cb1-7" title="7">...</a>
<a class="sourceLine" id="cb1-8" title="8">END not to filter</a>
<a class="sourceLine" id="cb1-9" title="9">...</a>
<a class="sourceLine" id="cb1-10" title="10">text</a>
<a class="sourceLine" id="cb1-11" title="11">...</a></code></pre></div>
</div>
<p>I searched a better way to do that, but the best I can do is using <code>split</code> and <code>scan</code>.</p>
<div>
<div class="sourceCode" id="cb2"><pre class="sourceCode ruby"><code class="sourceCode ruby"><a class="sourceLine" id="cb2-1" title="1"><span class="kw">def</span> allExceptCode( f, content )</a>
<a class="sourceLine" id="cb2-2" title="2"> <span class="co"># Beware the behaviour will change if you add</span></a>
<a class="sourceLine" id="cb2-3" title="3"> <span class="co"># parenthesis (groups) to the regexp!</span></a>
<a class="sourceLine" id="cb2-4" title="4"> regexp=<span class="ot">/&lt;code[^&gt;]*&gt;.*?&lt;\/code&gt;|&lt;pre[^&gt;]*&gt;.*?&lt;\/pre&gt;/m</span></a>
<a class="sourceLine" id="cb2-5" title="5"> tmp=<span class="st">&quot;&quot;</span></a>
<a class="sourceLine" id="cb2-6" title="6"> mem=[]</a>
<a class="sourceLine" id="cb2-7" title="7"> content.scan(regexp).each <span class="kw">do</span> |c|</a>
<a class="sourceLine" id="cb2-8" title="8"> mem &lt;&lt;= c</a>
<a class="sourceLine" id="cb2-9" title="9"> <span class="kw">end</span></a>
<a class="sourceLine" id="cb2-10" title="10"> i=<span class="dv">0</span></a>
<a class="sourceLine" id="cb2-11" title="11"> content.split(regexp).each <span class="kw">do</span> |x|</a>
<a class="sourceLine" id="cb2-12" title="12"> tmp &lt;&lt;= send(f,x) </a>
<a class="sourceLine" id="cb2-13" title="13"> <span class="kw">if</span> <span class="kw">not</span> mem[i].nil? </a>
<a class="sourceLine" id="cb2-14" title="14"> tmp &lt;&lt;= mem[i]</a>
<a class="sourceLine" id="cb2-15" title="15"> i+=<span class="dv">1</span></a>
<a class="sourceLine" id="cb2-16" title="16"> <span class="kw">end</span></a>
<a class="sourceLine" id="cb2-17" title="17"> <span class="kw">end</span></a>
<a class="sourceLine" id="cb2-18" title="18"> tmp</a>
<a class="sourceLine" id="cb2-19" title="19"><span class="kw">end</span></a></code></pre></div>
</div>
<p>An usage is:</p>
<div>
<div class="sourceCode" id="cb3"><pre class="sourceCode ruby"><code class="sourceCode ruby"><a class="sourceLine" id="cb3-1" title="1"><span class="kw">def</span> filter(content)</a>
<a class="sourceLine" id="cb3-2" title="2"> content.gsub(<span class="ot">/e/</span>,<span class="ch">'X'</span>)</a>
<a class="sourceLine" id="cb3-3" title="3"><span class="kw">end</span></a>
<a class="sourceLine" id="cb3-4" title="4">...</a>
<a class="sourceLine" id="cb3-5" title="5">allExceptCode(<span class="st">:filter</span>, content)</a>
<a class="sourceLine" id="cb3-6" title="6">...</a></code></pre></div>
</div>
<p>A better syntax would be:</p>
<div>
<div class="sourceCode" id="cb4"><pre class="sourceCode ruby"><code class="sourceCode ruby"><a class="sourceLine" id="cb4-1" title="1"><span class="co"># !!!!!!!!!! THIS SYNTAX DOES NOT WORK !!!!!!! #</span></a>
<a class="sourceLine" id="cb4-2" title="2"><span class="kw">def</span> allExceptCode( f, content )</a>
<a class="sourceLine" id="cb4-3" title="3"> regexp=<span class="ot">/&lt;code[^&gt;]*&gt;.*?&lt;\/code&gt;/m</span></a>
<a class="sourceLine" id="cb4-4" title="4"> tmp=<span class="st">&quot;&quot;</span></a>
<a class="sourceLine" id="cb4-5" title="5"> content.split(regexp).each <span class="kw">do</span> |x|</a>
<a class="sourceLine" id="cb4-6" title="6"> separator=<span class="dt">$&amp;</span></a>
<a class="sourceLine" id="cb4-7" title="7"> tmp &lt;&lt;= send(f,x) </a>
<a class="sourceLine" id="cb4-8" title="8"> <span class="kw">if</span> <span class="kw">not</span> separator.nil?</a>
<a class="sourceLine" id="cb4-9" title="9"> tmp &lt;&lt;= separator</a>
<a class="sourceLine" id="cb4-10" title="10"> <span class="kw">end</span></a>
<a class="sourceLine" id="cb4-11" title="11"> <span class="kw">end</span></a>
<a class="sourceLine" id="cb4-12" title="12"> tmp</a>
<a class="sourceLine" id="cb4-13" title="13"><span class="kw">end</span></a></code></pre></div>
</div>
<p>I would expect the split make a search on a regular expression and then give the matched expression into the <code>$&amp;</code> variable. But it is not the case.</p>
<p>If someone know a nicer way to do that I will be happy to know how.</p>
</div>
<div id="afterarticle">
<div id="social">
<a href="/rss.xml" target="_blank" rel="noopener noreferrer nofollow" class="social">RSS</a>
·
<a href="https://twitter.com/home?status=http%3A%2F%2Fyannesposito.com/Scratch/en/blog/2009-09-replace-all-except-some-part/%20via%20@yogsototh" target="_blank" rel="noopener noreferrer nofollow" class="social">Tweet</a>
·
<a href="http://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fyannesposito.com/Scratch/en/blog/2009-09-replace-all-except-some-part/" target="_blank" rel="noopener noreferrer nofollow" class="social">FB</a>
<br />
<a class="message" href="../../../../Scratch/en/blog/Social-link-the-right-way/">These social sharing links preserve your privacy</a>
</div>
<div id="navigation">
<a href="../../../../">Home</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/blog">Blog</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/softwares">Softwares</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/about">About</a>
</div>
<div id="totop"><a href="#header">↑ Top ↑</a></div>
<div id="bottom">
<div>
Published on 2009-09-22
</div>
<div>
<a href="https://twitter.com/yogsototh">Follow @yogsototh</a>
</div>
<div>
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/deed.en_US">Yann Esposito©</a>
</div>
<div>
Done with
<a href="http://www.vim.org" target="_blank" rel="noopener noreferrer nofollow"><strike>Vim</strike></a>
<a href="http://spacemacs.org" target="_blank" rel="noopener noreferrer nofollow">spacemacs</a>
<span class="pala">&amp;</span>
<a href="http://nanoc.ws" target="_blank" rel="noopener noreferrer nofollow"><strike>nanoc</strike></a>
<a href="http://jaspervdj.be/hakyll" target="_blank" rel="noopener noreferrer nofollow">Hakyll</a>
</div>
<hr />
<div style="max-width: 100%">
<a href="https://cardanohub.org">
<img src="../../../../Scratch/img/ada-logo.png" class="simple" style="height: 16px;
border-radius: 50%;
vertical-align:middle;
display:inline-block;" />
ADA:
</a>
<code style="display:inline-block;
word-wrap:break-word;
text-align: left;
vertical-align: top;
max-width: 85%;">
DdzFFzCqrhtAvdkmATx5Fm8NPJViDy85ZBw13p4XcNzVzvQg8e3vWLXq23JQWFxPEXK6Kvhaxxe7oJt4VMYHxpA2vtCFiP8fziohN6Yp
</code>
</div>
</div>
</div>
</div>
</div>
</body>
</html>