|
Mailing Lists
|
Home /
Groups /
Regular Expressions (RegEx)
Break up an anchor tag
I did not get any responses to yesterday's question so here it is again,Ian Skinner 01/16/09 11:48 A Hi Ian, I've cc-ed you directy incase the HoF emailing doesn't work again.Peter Boughton 01/16/09 03:59 P Peter Boughton wrote:Ian Skinner 01/16/09 05:04 P 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 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. Peter Boughton wrote: ----- Excess quoted text cut - see Original Post for more ----- 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
|
June 18, 2013
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||