|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
How to search for all occurences of a string in a text?
Hi,marc -- 11/18/11 07:32 P Start by simplifying the regex:Jason Fisher 11/18/11 08:22 P Thanks, it works now!marc -- 11/19/11 07:18 A Hi, I have a series of texts, each of which I want to search for the occurence of links to images. From each text I want to extract image filenames that end on one of these strings: .gif,.jpg,.jpeg,.png,.bmp Searching a text containing the string <img src="http://my.domain.com/path/to/images/testimg.gif"> would give me testimg.gif, searching a text containing the strings <img src="http://my.domain.com/path/to/images/testimg.gif"> <img src="http://my.domain.com/path/to/images/testimg.jpg"> <img src="http://my.domain.com/path/to/images/testimg.png"> would give me testimg.gif,testimg.jpg,testimg.png I think REFindNoCase is most suitable for this since it's the only CF function that lets me search for multiple occurences of a pattern I know of. I can't get it to match _all_ occurences of the string I'm looking for. The regular expression I use: (<img src ?= ?[""|'][https?://]?[a-zA-Z0-9_\-/\\\.^\.jpg]+\.jpg)\s?(\1)* I do this: REFindNoCase(RE,string,1,"true") where RE is the above regular expression and string is the string containing 3 <img> tags with an .jpg resource. This gives me an array containing the starting position and the length of the _first_ element in the string that matches with the regular expression. I can't get it to include all matches in the string. Is my regular expression wrong or is it not possible what I want? Marc Start by simplifying the regex: <cfset reg = """[^""]+/([^""/]+\.(jpg|gif|png))""" /> (It just gets every image reference inside double-quotes.) Then use REMatchNoCase() to get an array of all matches: <cfset test = reMatchNoCase(reg, string) /> (REFindNoCase() only gets one match, so it only works if you put it inside a conditional loop.) Then loop over the array and get the last "list" element using the "/" as the delimiter (and also strip out the trailing quote): <cfoutput> <cfloop array="#test#" index="a"> #listLast(listLast(a, """"), "/")# <br /> </cfloop> </cfoutput> On 11/18/2011 7:32 PM, marc -- wrote: ----- Excess quoted text cut - see Original Post for more ----- Thanks, it works now!
|
May 22, 2013
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||