ColdFusion / Regular Expression / Returning backreferences into an array
Snippet details
ID: 490
Viewed: 7464
Added: 2002-09-09
Version:
This is a custom CFML tag which will return a pattern matched string to an array, with each backreference occupying an element of the array and the whole matched string occupying the first element.. This emulates the preg_match() function of PHP.
Instructions are to copy and paste the custom tag code a file. Copy this file to either the folder from where it will be invoked or the custom tag folder. View the samples which show how the tag should be used. Any questions, post them on www.htmlforums.com and I'll pop along and sort them out.
Instructions are to copy and paste the custom tag code a file. Copy this file to either the folder from where it will be invoked or the custom tag folder. View the samples which show how the tag should be used. Any questions, post them on www.htmlforums.com and I'll pop along and sort them out.
<!---Head--->
none
<!---Body--->
<!--- START COPY AND PASTE FOR CUSTOM TAG --->
<!---
Description: A custom tag to perform the equivalent of PHP's preg_match() function.
This will take a regular expression along with a string and return any
backreferences in an array.
preg_match[1] = the complete pattern-matched string
- FALSE is returned if no match is found
- *ERROR REPORTED* is returned if the operation fails
preg_match[2...n] = strings returned through the backreferences.
Entering: N/A
Exiting: N/A
Dependencies: N/A
Expecting: pattern : the regular expression pattern
line : the string against which the pattern will be compared
Modification History:
Date Modifier Change
********************************************************
07-Sep-02 Torrent Created
--->
<cftry>
<!--- Check if variables are defined --->
<cfif isDefined("attributes.pattern") and isDefined("attributes.line")>
<!-- Initialise the preg_match array -->
<cfset caller.preg_match = arrayNew(1) />
<cfset caller.preg_match[1] = FALSE />
<!-- Nuff talk, let's get down to business --->
<cfscript>
if (REFind(attributes.pattern, attributes.line, 1)) {
match = REFind(attributes.pattern, attributes.line, 1, TRUE);
maxLoop = arrayLen(match.pos);
for (idx = 1; idx lte arrayLen(match.pos); idx = idx + 1) {
// Note: The toString() is required in case a match returns -1, which could
// otherwise be interpreted as FALSE in the calling template
caller.preg_match[idx] = toString(mid(attributes.line, match.pos[idx], match.len[idx]));
}
}
</cfscript>
<!--- End of Perform function --->
<cfelse>
<!--- Not all the required attributes were passed through <cfmodule> --->
<cfthrow
type ="UndefinedAttributes"
detail="<b>Missing required attributes</b><br />
Required attributes are:<br />pattern (type = string, content = regular expression)<br />
line (type = string, content = the string to perform the regular expression against)<br />"
/>
<cfset caller.preg_match[1] = "*ERROR REPORTED*" />
</cfif>
<!--- Error handler --->
<cfcatch type="UndefinedAttributes">
<cfoutput>#cfcatch.detail#</cfoutput>
</cfcatch>
</cftry>
<!--- END CUSTOM TAG COPY AND PASTE --->
<!--- START OF SAMPLE CODE (copy to a new file) --->
<html>
<head>
<title>Sample use for preg_match custom tag</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<!--- Simple Example --->
<cfmodule template="cust_PregMatch.cfm"
pattern="([a-zA-Z]+)[-]([0-9]+)"
line="ABC-123"
/>
<!--- OUTPUT RESULTS --->
<cfset maxLoop=arrayLen(preg_match) />
<cfloop index="idx" from="1" to="#maxLoop#">
<cfoutput>Preg_Match<[#idx#] = [#preg_match[idx]#]<br /></cfoutput>
</cfloop>
<!--- /////////////////////////////////////////////////////////// --->
<!--- More complex (virtual) example --->
<cffile action="read" file="d:apachelogsaccess.log" variable="test">
<cfset arrFile = listToArray(test, "#chr(13)##chr(10)#") />
<cfmodule template="cust_PregMatch.cfm"
pattern='(S+)s(S+)s(S+)s[[](.+)[]]s["](.+)["]s(S+)s(S+)s["](.*)["]s["](.+)["]'
line="#arrFile[1]#"
/>
<cfif Preg_Match[1] NEQ FALSE>
<!--- OUTPUT RESULTS --->
<cfset maxLoop=arrayLen(preg_match) />
<cfloop index="idx" from="1" to="#maxLoop#">
<cfoutput>Preg_Match<[#idx#] = [#preg_match[idx]#]<br /></cfoutput>
</cfloop>
<cfelse>
No matches found.
</cfif>
</body>
</html>
No Reviews to show
