parse url and title from string of multiple href tags in coldfusion
i need to parse the url and title from multiple href tags in a string regex... i need to get each url and title into a variable
eg.<DT><A HREF="http://www.partyboatnj.com/" ADD_DATE="1210713679" LAST_VISIT="1225055180" LAST_MODIFIED="1210713679">NJ Party Boat - Sea Devil of Point Pleasant Beach, NJ</A>
<DT><A HREF="http://www.test.com/" ADD_DATE="1210713679" LAST_VISIT="1225055180" LAST_MODIFIED="1210713679">test parse</A>
<DT><A HREF=开发者_StackOverflow中文版"http://www.google.com/" ADD_DATE="1210713679" LAST_VISIT="1225055180" LAST_MODIFIED="1210713679">google</A>
Ok, if I understand correctly, I would do something like this:
<cffunction name="reMatchGroups" access="public" returntype="array" output="false">
<cfargument name="text" type="string" required="true" />
<cfargument name="pattern" type="string" required="true" />
<cfargument name="scope" type="string" required="false" default="all" />
<cfscript>
l = {};
l.results = [];
l.pattern = createObject("java", "java.util.regex.Pattern").compile(javacast("string", arguments.pattern));
l.matcher = l.pattern.matcher(javacast("string", arguments.text));
while(l.matcher.find()) {
l.groups = {};
for(l.i = 1; l.i <= l.matcher.groupCount(); l.i++) {
l.groups[l.i] = l.matcher.group(javacast("int", l.i));
}
arrayAppend(l.results, l.groups);
if(arguments.scope == "one")
break;
}
return l.results;
</cfscript>
</cffunction>
The above function returns groups for each regex pattern match.
You could use it like this:
<cfset a = reMatchGroups("<a href=""http://iamalink.com"" class=""testlink"">This is a link</a>", "href=[""']([^""|']*)[""'][^>]*>([^<]*)", "all") />
Which will give you an array of structs with the key-value pairs for each back reference in the regex. In this case the href and node text.
精彩评论