tags:

views:

108

answers:

4

Hi all

I have this html with this type of snippit below all over:

<li><label for="summary">Summary:</label></li>
<li class="in">
    <textarea class="ta" id="summary" name="summary" rows="4" cols="10" tabindex="4">
        ${fieldValue(bean: book, field: 'summary')}</textarea> 

    <a href="#" class="tt">
        <img src="<g:createLinkTo dir='images/buttons/' file='icon.gif'/>" alt="Help icon for the summary field">
        <span class="tooltip">
            <span class="top"></span>
            <span class="middle">Help text for summary</span>
            <span class="bottom"></span>
        </span>
    </a>
</li>

I want to pull off the alt value and the text between XXXX and replace the a tag with the code below.

This is my stab at the reg ex

<a href="#" class="tt">.*alt="(.*)".*<span class="middle">(.*)<\/span><\/a>

Output with the callbacks

<ebs:cssToolTip alt="$1" text="$2"/>

I tried it out on http://rubular.com/ and it does not quite work. Any suggestions

+1  A: 

You may want to ensure your regexp isn't greedily picking up characters - use ".*?" rather than straight ".*".

Matthew Iselin
+1  A: 

What do you mean, "it does not quite work"? How does it fail?

A suggestion (not tested your regexp): note that * is a greedy operator, so .* is rarely a good idea because it may match a lot more than what you intended.

Try:

<a href="#" class="tt">.*alt="([^"]*)".*<span class="middle">([^"]*)<\/span><\/a>
cadrian
+1  A: 

Think i solved it by getting an idea from another stackoverflow question

<a href="#" class="tt">.*alt="([^"]*)".*<span class="middle">([^<]*).*<\/a>

This seems to work on the http://rubular.com/ site

Peter Delahunty
This achieves the same as .*? - for example: `alt="(.*?)"` will match everything up to the next ", but `alt="(.*)"` will match everything to last "
dbr
A: 

Here you go: http://rubular.com/regexes/8434

You were facing two potential problems. First, without adding the //m option, '.' will not match newline characters. Second, you were using greedy matching. Adding the '*?' makes it better.

/<a href="#" class="tt">.*?alt="([^"]*)">.*?<span class="middle">(.*?)<\/span>/m
Eric