So, I''m trying to write a nice bit of regex to handle finding anchor tags in a bit of html. This is what I''ve got.... /<[aA][^>]*>[^<]*<\/[aA]>/ I''m planning on using this with a gsub!. Here is what it has to do.... <html><a href="http://stuff.com" class="link">Anything in here.</a></html> As you can see, the regex I have will indeed work for this instance. It will match with ''<a href="http://stuff.com" class="link">Anything in here.</a>'' Except, here is where it fails.... <html><a href="http://stuff.com" class="link"><strong>Anything</strong> in here.</a></html> I still need ''<a href="http://stuff.com" class="link"><strong>Anything</strong> in here.</a>'' This regex doesn''t work... /<[aA][^>]*>.*<\/[aA]>/ Because my middle tag will just keep going and return <a href="http://stuff.com" class="link"><strong>Anything</strong> in here.</a></html> because it doesn''t have any reason to stop. Any suggestions? -hampton. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060424/d861818c/attachment.html
replace .* with [^(<\/a)(<\/A)] That should work. -N On 24/04/06, Hampton <hcatlin@gmail.com> wrote:> So, I''m trying to write a nice bit of regex to handle finding anchor tags in > a bit of html. > > This is what I''ve got.... > > /<[aA][^>]*>[^<]*<\/[aA]>/ > > I''m planning on using this with a gsub!. > > Here is what it has to do.... > > <html><a href="http://stuff.com" class="link">Anything in here.</a></html> > > As you can see, the regex I have will indeed work for this instance. It will > match with ''<a href=" http://stuff.com" class="link">Anything in here.</a>'' > > Except, here is where it fails.... > > <html><a href="http://stuff.com " > class="link"><strong>Anything</strong> in here.</a></html> > > I still need ''<a href="http://stuff.com" > class="link"><strong>Anything</strong> in here.</a>'' > > This regex doesn''t work... > /<[aA][^>]*>.*<\/[aA]>/ > Because my middle tag will just keep going and return > > <a href="http://stuff.com" > class="link"><strong>Anything</strong> in here.</a></html> > > because it doesn''t have any reason to stop. > > Any suggestions? > > -hampton. > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
You could use the ? non-greedy modifier for this: /<a.*?<\/a>/ This will match from the beginning of the <a to the first </a>. On 4/23/06, Hampton <hcatlin@gmail.com> wrote:> So, I''m trying to write a nice bit of regex to handle finding anchor tags in > a bit of html. > > This is what I''ve got.... > > /<[aA][^>]*>[^<]*<\/[aA]>/ > > I''m planning on using this with a gsub!. > > Here is what it has to do.... > > <html><a href="http://stuff.com" class="link">Anything in here.</a></html> > > As you can see, the regex I have will indeed work for this instance. It will > match with ''<a href=" http://stuff.com" class="link">Anything in here.</a>'' > > Except, here is where it fails.... > > <html><a href="http://stuff.com " > class="link"><strong>Anything</strong> in here.</a></html> > > I still need ''<a href="http://stuff.com" > class="link"><strong>Anything</strong> in here.</a>'' > > This regex doesn''t work... > /<[aA][^>]*>.*<\/[aA]>/ > Because my middle tag will just keep going and return > > <a href="http://stuff.com" > class="link"><strong>Anything</strong> in here.</a></html> > > because it doesn''t have any reason to stop. > > Any suggestions? > > -hampton. > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Family management on rails: http://www.famundo.com - coming soon! My development related blog: http://devblog.famundo.com
WOooooowhoooo!!!! Thanks! On 4/23/06, Just Someone <just.some@gmail.com> wrote:> > You could use the ? non-greedy modifier for this: > > /<a.*?<\/a>/ > > This will match from the beginning of the <a to the first </a>. > > > On 4/23/06, Hampton <hcatlin@gmail.com> wrote: > > So, I''m trying to write a nice bit of regex to handle finding anchor > tags in > > a bit of html. > > > > This is what I''ve got.... > > > > /<[aA][^>]*>[^<]*<\/[aA]>/ > > > > I''m planning on using this with a gsub!. > > > > Here is what it has to do.... > > > > <html><a href="http://stuff.com" class="link">Anything in > here.</a></html> > > > > As you can see, the regex I have will indeed work for this instance. It > will > > match with ''<a href=" http://stuff.com" class="link">Anything in > here.</a>'' > > > > Except, here is where it fails.... > > > > <html><a href="http://stuff.com " > > class="link"><strong>Anything</strong> in here.</a></html> > > > > I still need ''<a href="http://stuff.com" > > class="link"><strong>Anything</strong> in here.</a>'' > > > > This regex doesn''t work... > > /<[aA][^>]*>.*<\/[aA]>/ > > Because my middle tag will just keep going and return > > > > <a href="http://stuff.com" > > class="link"><strong>Anything</strong> in here.</a></html> > > > > because it doesn''t have any reason to stop. > > > > Any suggestions? > > > > -hampton. > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > -- > Family management on rails: http://www.famundo.com - coming soon! > My development related blog: http://devblog.famundo.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060424/9ae8da0f/attachment.html