0

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
  • Added whitespace for readability Commented Jun 26, 2009 at 13:32

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1

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

1 Comment

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.