Regex Question

N

NotYetaNurd

I have a text like this
<a href=aaaa>something </a><a href=aaaa>something </a><a href=aaaa>something
</a><a href=aaaa>something </a><a href=aaaa>something </a>
i have to extract each "<a href=aaaa>something </a> "out i did something
like this
<A[^>]*>.*</A>

but it returns the whole string rather than individual "<a
href=aaaa>something </a>"

Where am i going wrong.....
 
A

Arild Bakken

Try the following expression:

<A[^>]*>.*?</A>

Notice the question mark to indicate a "lazy" matching so that it stops at
the first occurence of </A> and not at the last on the line.


Arild
 
J

Jon Lynch

The .* term is gobbling up the whole string. Use .*? (non-greedy gobble) or
just [^<]*.

There is a tradeoff, as always. .*? is slightly more expensive at
run-time, but it's a little clearer.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top