<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Ruby, sustituyendo matches de una regex en un String con matches de la misma regex</title>
	<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/</link>
	<description>software development as an artistic expression</description>
	<pubDate>Thu, 11 Mar 2010 09:37:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.2</generator>
		<item>
		<title>By: el verdor</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-835</link>
		<dc:creator>el verdor</dc:creator>
		<pubDate>Sun, 15 Nov 2009 23:41:05 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-835</guid>
		<description>Hola, y como lo harías si tienes dos select en un string, y quieres sacar primero un contenido y luego el otro</description>
		<content:encoded><![CDATA[<p>Hola, y como lo harías si tienes dos select en un string, y quieres sacar primero un contenido y luego el otro</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fguillen</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-699</link>
		<dc:creator>fguillen</dc:creator>
		<pubDate>Sun, 12 Jul 2009 15:32:26 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-699</guid>
		<description>En realidad la solución buena es usar el 'xxx \1' con comillas simples:

&#62;&#62; "me gusta el musical y no me gusta el flamenco".gsub( /me gusta (.*) y no me gusta el (.*)/, 'me gusta \2 y no me gusta el \1' )
=&#62; "me gusta flamenco y no me gusta el el musical"</description>
		<content:encoded><![CDATA[<p>En realidad la solución buena es usar el &#8216;xxx \1&#8242; con comillas simples:</p>
<p>&gt;&gt; &#8220;me gusta el musical y no me gusta el flamenco&#8221;.gsub( /me gusta (.*) y no me gusta el (.*)/, &#8216;me gusta \2 y no me gusta el \1&#8242; )<br />
=&gt; &#8220;me gusta flamenco y no me gusta el el musical&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Balint Erdi</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-416</link>
		<dc:creator>Balint Erdi</dc:creator>
		<pubDate>Fri, 05 Sep 2008 12:26:18 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-416</guid>
		<description>Porque dos centimos? Dos cientos son mejor, no? ;)</description>
		<content:encoded><![CDATA[<p>Porque dos centimos? Dos cientos son mejor, no? ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Balint Erdi</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-415</link>
		<dc:creator>Balint Erdi</dc:creator>
		<pubDate>Fri, 05 Sep 2008 12:24:52 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-415</guid>
		<description>Ya pensaba que hay solucion mas elegante que la mia, sino no seria ruby :) (Gracias, Aitor)</description>
		<content:encoded><![CDATA[<p>Ya pensaba que hay solucion mas elegante que la mia, sino no seria ruby :) (Gracias, Aitor)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fguillen</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-406</link>
		<dc:creator>fguillen</dc:creator>
		<pubDate>Fri, 05 Sep 2008 09:23:56 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-406</guid>
		<description>&lt;b&gt;Aitor&lt;/b&gt;, increíblemente instructivos tus ejemplos de malabarismos con regexs, no se me habrían ocurrido :)</description>
		<content:encoded><![CDATA[<p><b>Aitor</b>, increíblemente instructivos tus ejemplos de malabarismos con regexs, no se me habrían ocurrido :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fguillen</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-405</link>
		<dc:creator>fguillen</dc:creator>
		<pubDate>Fri, 05 Sep 2008 09:22:43 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-405</guid>
		<description>&lt;b&gt;Balint&lt;/b&gt;, tienes toda la razón de que el gsub no era buena solución para mi intento de extraer el contenido de una tag xml, además de lioso no respondía bien pues en caso de no encontrar ningún match con la regex devolvía todo el string y no es correcto.

Si no hay match no se debería devolver nada.

De hecho, al final mi metodo extractor de bodys queda así:

&lt;pre lang="ruby"&gt;
def get_body_content string
  string.match( /.*&lt;\s*body[^&gt;]*&gt;(.*)&lt;\/body\s*&gt;.*/mi )
  $1.nil? ? nil : $1.strip
end
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p><b>Balint</b>, tienes toda la razón de que el gsub no era buena solución para mi intento de extraer el contenido de una tag xml, además de lioso no respondía bien pues en caso de no encontrar ningún match con la regex devolvía todo el string y no es correcto.</p>
<p>Si no hay match no se debería devolver nada.</p>
<p>De hecho, al final mi metodo extractor de bodys queda así:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> get_body_content <span style="color:#CC0066; font-weight:bold;">string</span>
  <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">&lt;</span>\s<span style="color:#006600; font-weight:bold;">*</span>body<span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">*&gt;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&lt;</span>\<span style="color:#006600; font-weight:bold;">/</span>body\s<span style="color:#006600; font-weight:bold;">*&gt;</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">/</span>mi <span style="color:#006600; font-weight:bold;">&#41;</span>
  $<span style="color:#006666;">1</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>? ? <span style="color:#0000FF; font-weight:bold;">nil</span> : $<span style="color:#006666;">1</span>.<span style="color:#9900CC;">strip</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: fguillen</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-404</link>
		<dc:creator>fguillen</dc:creator>
		<pubDate>Fri, 05 Sep 2008 09:20:33 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-404</guid>
		<description>&lt;b&gt;Balint&lt;/b&gt;, &lt;b&gt;Aitor&lt;/b&gt;: Os he reparado los comentarios pues el cabroncete del wordpress se come muchos caracteres importantes. Espero no haber metido la pata :)</description>
		<content:encoded><![CDATA[<p><b>Balint</b>, <b>Aitor</b>: Os he reparado los comentarios pues el cabroncete del wordpress se come muchos caracteres importantes. Espero no haber metido la pata :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fguillen</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-403</link>
		<dc:creator>fguillen</dc:creator>
		<pubDate>Fri, 05 Sep 2008 09:11:25 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-403</guid>
		<description>&lt;b&gt;Balint&lt;/b&gt;: Mi dos cientos de euro =&gt; Mis dos céntimos de euro :)</description>
		<content:encoded><![CDATA[<p><b>Balint</b>: Mi dos cientos de euro => Mis dos céntimos de euro :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aitor</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-402</link>
		<dc:creator>Aitor</dc:creator>
		<pubDate>Fri, 05 Sep 2008 08:21:56 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-402</guid>
		<description>IMHO hay formas más directas para el caso de obtener un substring:

&lt;pre lang="ruby"&gt;
&lt;body&gt;contenido&lt;/body&gt;"[/REGEXP/, 1]
&lt;/pre&gt;

&lt;pre lang="ruby"&gt;
/REGEXP/.match( "&lt;body&gt;contenido&lt;/body&gt;")[1]
&lt;/pre&gt;


Los backreferences (\1, \2...) son más útiles y están pensados para cuando se usan dentro de la propia REGEXP:

&lt;pre lang="ruby"&gt;
"abbc"[/(.)\1/] -&gt; "bb" #matchea dos caracteres iguales seguidos
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>IMHO hay formas más directas para el caso de obtener un substring:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>contenido<span style="color:#006600; font-weight:bold;">&lt;/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#996600;">&quot;[/REGEXP/, 1]</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#006600; font-weight:bold;">/</span>REGEXP<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;&lt;body&gt;contenido&lt;/body&gt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Los backreferences (\1, \2&#8230;) son más útiles y están pensados para cuando se usan dentro de la propia REGEXP:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#996600;">&quot;abbc&quot;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">&#41;</span>\<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#996600;">&quot;bb&quot;</span> <span style="color:#008000; font-style:italic;">#matchea dos caracteres iguales seguidos</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Balint Erdi</title>
		<link>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-400</link>
		<dc:creator>Balint Erdi</dc:creator>
		<pubDate>Fri, 05 Sep 2008 08:11:16 +0000</pubDate>
		<guid>http://www.fernandoguillen.info/es/blog/2008/09/04/ruby-sustituyendo-matches-de-una-regex-en-un-string-con-matches-de-la-misma-regex/#comment-400</guid>
		<description>Hola Fernando, estoy disfrutando de tus posts sobre Ruby y aprendiendo contigo. Mi dos cientos de euro (un anglisismo?) es que quizas sea mas intuitivo usar scan or =~ cuando quieres sacar algo del string i no sustituir.

Asi tu ejemplo seria:

&lt;pre lang="ruby"&gt;
"&lt;body&gt;contenido&lt;/body&gt;".scan( /.*&lt;body[^&gt;]*&gt;(.*)&lt;\/body&gt;.*/mix) { &#124;m&#124; (... hacer algo con 'contenido' ... }
&lt;/pre&gt;

o bien:

&lt;pre lang="ruby"&gt;
"&lt;body&gt;contenido&lt;/body&gt;".scan( /.*&lt;body[^&gt;]*&gt;(.*)&lt;\/body&gt;.*/mix).first.first.strip #ya, es un poco fea, parece que scan en este forma sea un overkill)
&lt;/pre&gt;

Con =~ :

&lt;pre lang="ruby"&gt;
"&lt;body&gt;contenido&lt;/body&gt;" =~ /.*&lt;body[^&gt;]*&gt;(.*)&lt;\/body&gt;.*/mix
&lt;/pre&gt;

y tienes 'contenido' en &lt;strong&gt;$1&lt;/strong&gt;.

Keep your Ruby posts coming!
Balint</description>
		<content:encoded><![CDATA[<p>Hola Fernando, estoy disfrutando de tus posts sobre Ruby y aprendiendo contigo. Mi dos cientos de euro (un anglisismo?) es que quizas sea mas intuitivo usar scan or =~ cuando quieres sacar algo del string i no sustituir.</p>
<p>Asi tu ejemplo seria:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#996600;">&quot;&lt;body&gt;contenido&lt;/body&gt;&quot;</span>.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">*&gt;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&lt;</span>\<span style="color:#006600; font-weight:bold;">/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">/</span>mix<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span>... <span style="color:#9900CC;">hacer</span> algo con <span style="color:#996600;">'contenido'</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>o bien:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#996600;">&quot;&lt;body&gt;contenido&lt;/body&gt;&quot;</span>.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">*&gt;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&lt;</span>\<span style="color:#006600; font-weight:bold;">/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">/</span>mix<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">strip</span> <span style="color:#008000; font-style:italic;">#ya, es un poco fea, parece que scan en este forma sea un overkill)</span></pre></div></div>

<p>Con =~ :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#996600;">&quot;&lt;body&gt;contenido&lt;/body&gt;&quot;</span> =~ <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">*&gt;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&lt;</span>\<span style="color:#006600; font-weight:bold;">/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>.<span style="color:#9900CC;">*</span><span style="color:#006600; font-weight:bold;">/</span>mix</pre></div></div>

<p>y tienes &#8216;contenido&#8217; en <strong>$1</strong>.</p>
<p>Keep your Ruby posts coming!<br />
Balint</p>
]]></content:encoded>
	</item>
</channel>
</rss>
