472,141 Members | 1,026 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,141 software developers and data experts.

Regex Question

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.....

Nov 13 '05 #1
3 4364
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

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...
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.....

Nov 13 '05 #2

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

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...
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.....

Nov 13 '05 #3
Thanks You ...
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...
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.....

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by engwar1 | last post: by
2 posts views Thread by Tim Conner | last post: by
6 posts views Thread by Du Dang | last post: by
17 posts views Thread by clintonG | last post: by
5 posts views Thread by Chris | last post: by
6 posts views Thread by Martin Evans | last post: by
7 posts views Thread by Extremest | last post: by
6 posts views Thread by Phil Barber | last post: by
6 posts views Thread by | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.