Last active
November 25, 2015 00:01
-
-
Save bmdoherty/83cab22dfcd0b2171af3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction | |
name="REMatchGroup" | |
access="public" | |
returntype="array" | |
output="false" | |
hint="Gets the given group of a captured expression."> | |
<!--- Define arguments. ---> | |
<cfargument | |
name="Pattern" | |
type="string" | |
required="true" | |
hint="The regular expression we want to match on." | |
/> | |
<cfargument | |
name="Text" | |
type="string" | |
required="true" | |
hint="The target text against which we will run the patterns." | |
/> | |
<cfargument | |
name="Group" | |
type="numeric" | |
required="false" | |
default="0" | |
hint="The captured group we want to return." | |
/> | |
<!--- Define the local scope. ---> | |
<cfset var LOCAL = {} /> | |
<!--- | |
Create the results array. Here is where we will store | |
our captured groups. | |
---> | |
<cfset LOCAL.Results = [] /> | |
<!--- | |
Compile our regular expression pattern. By using the | |
underlying Java pattern class, we will have both faster | |
pattern matching and more access to the matched groups. | |
---> | |
<cfset LOCAL.Pattern = CreateObject( | |
"java", | |
"java.util.regex.Pattern" | |
).Compile( | |
JavaCast( "string", ARGUMENTS.Pattern ) | |
) | |
/> | |
<!--- Get our pattern matcher for our target text. ---> | |
<cfset LOCAL.Matcher = LOCAL.Pattern.Matcher( | |
JavaCast( "string", ARGUMENTS.Text ) | |
) /> | |
<!--- | |
Keep looping over the pattern matches until we hit the | |
end of the target string. | |
---> | |
<cfloop condition="LOCAL.Matcher.Find()"> | |
<!--- | |
Append the target group to the results array. If the | |
Group was not defined (defaulting to zero), then the | |
entire pattern will be matched. | |
---> | |
<cfset ArrayAppend( | |
LOCAL.Results, | |
LOCAL.Matcher.Group( | |
JavaCast( "int", ARGUMENTS.Group ) | |
) | |
) /> | |
</cfloop> | |
<!--- Return the results array. ---> | |
<cfreturn LOCAL.Results /> | |
</cffunction> | |
<cfset arrURLs = REMatchGroup( | |
"application\/vnd.mywebsite\+([A-Za-z]+);\s*version=(\d+)", | |
"application/vnd.mywebsite+json; version=1", | |
1 | |
) /> | |
<cfdump var='#arrURLs#'> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment