Break up an anchor tag.

View: New views
3 Messages — Rating Filter:   Alert me  

Break up an anchor tag.

by Ian Skinner-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I did not get any responses to yesterday's question so here it is again,
rephrased.

Using ColdFusion 8's regex functionality how could I capture everything
before a target property, the target property - if it exists- and
everything after the target property.

I can do this, when the target property exists with this regex, but this
fails if the target property does not exist.
<(/?)a([^>]*)(target="[^"]*")([^>]*)

If I add anything to make the the target clause optional ?,*,??,*?, I
get unexpected behavior.  The first ([^]*) clause captures all the
properties of the anchor tag including the target property.  I want them
separated.

I've tried other methods to select everything before the target property
such as just .*? but nothing I have tried has worked.

P.S. this will need to work with strings that could contain multiple
anchor tags and should not grab content from more then one tag.

Thanks Ian


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to date
Get the Free Trial
http://ad.doubleclick.net/clk;207172674;29440083;f

Archive: http://www.houseoffusion.com/groups/regex/message.cfm/messageid:1211
Subscription: http://www.houseoffusion.com/groups/regex/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.21

Re: Break up an anchor tag.

by Peter Boughton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Ian, I've cc-ed you directy incase the HoF emailing doesn't work again.

> The first ([^]*) clause captures all the
> properties of the anchor tag including the target property.  I want them
> separated.

Hmm, well in theory you'd make the outer bits non-greedy, but when
you've effectively made all parts optional, I'm not sure about
specifying a "priority" for which optional one is most important...
<a([^>]*?)(target="[^"]*")?([^>]*?)>

That aside, your wording suggests you're attempting to get three
separate matches out of that (one for each parenthesis group), but
with rematch function you get an array of matches per expression, but
not an array of groups per match.

If you want that, then you need to grab each anchor, then loop through
and manually convert each one.

Along the lines of this...

<cfset Anchors = rematch( '<a[^>]+>' , Input )/>

<cfdump var="#Anchors#"/>

<hr/>

<cfset Delim = Chr(65536)/>

<cfloop index="i" from="1" to="#ArrayLen(Anchors)#">

        <cfset AnchorParts = rereplace( Anchors[i] ,
'<a([^>]*?)(target\s*=\s*"[^"]+")([^>]*?)>' , '\1#Delim#\2#Delim#\3' )
/>

        <cfdump var="#ListToArray( AnchorParts , Delim )#" label="#Anchors[i]#"/>

</cfloop>

<!--- (note the target attr is not optional - because of what the rest
of the code does it's not necessary here) --->


Assuming I'm not misunderstanding you entirely, this still all seems a
bit of an odd request - if you're able to expand more on the overall
problem you're trying to solve (not the regex side, but why are you
doing this, what is the relevance of target here, etc), then it might
be possible to suggest a better overall solution.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to date
Get the Free Trial
http://ad.doubleclick.net/clk;207172674;29440083;f

Archive: http://www.houseoffusion.com/groups/regex/message.cfm/messageid:1214
Subscription: http://www.houseoffusion.com/groups/regex/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.21

Re: Break up an anchor tag.

by Ian Skinner-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Peter Boughton wrote:
> Hi Ian, I've cc-ed you directy incase the HoF emailing doesn't work again.
>  
> Hmm, well in theory you'd make the outer bits non-greedy, but when
> you've effectively made all parts optional, I'm not sure about
> specifying a "priority" for which optional one is most important...
> <a([^>]*?)(target="[^"]*")?([^>]*?)>
>  

Thanks, I got this message.  It looks like I posted my own code in my
first thread
[http://www.houseoffusion.com/groups/regex/thread.cfm/threadid:232#1213]
about the same time as you posted yours here.  I knew I could fairly
easily develop code that would accomplish my goal if I broke it up into
several steps.  But as I am not very skilled at Regular Expressions yet
and this is just for a playtime project, I wanted to see how it could be
done with Regular Expressions as my intuition said it should be able to
be done.

If you care to review and critique my code I would be most happy to hear
what you might have to say.

Thanks for you help.
Ian

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to date
Get the Free Trial
http://ad.doubleclick.net/clk;207172674;29440083;f

Archive: http://www.houseoffusion.com/groups/regex/message.cfm/messageid:1216
Subscription: http://www.houseoffusion.com/groups/regex/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.21