Returning Matched Sub-Expressions  
 
 

Regular expressions in ColdFusion allow you to access matched sub-expressions using the REFind and REFindNoCase functions. If you set the fourth parameter, ReturnSubExpression, to TRUE, the function returns a CFML structure with two arrays containing the positions and lengths of the matched sub-expressions, if any.

You can find the structure's contents using the keys "pos" and "len". If there are no occurrences of the regular expression, the "pos" and the "len" arrays each contain 1 element with a value of 0.

 
 
  Example  
 
<CFSET subExprs=REFind("([A-Za-z]+)[ ]+\1",
    "There is is a cat in in the kitchen",1,"TRUE")>

<CFSET posarray = subExprs.pos>

<CFSET lenarray=subExprs.len>

After these statements, posarray[1]=7, lenarray[1]=6, posarray[2]=7, and lenarray[2]=2.

Note that posarray[1] and lenarray[1] refer to the entire matched expression ("is is"), while posarray[2] and lenarray[2] refer to the first parenthesized sub-expression. This is always the case, because the complete matched expression is returned in the first element and the parenthesized elements are returned sequentially from indices 2 onwards. Posarray[1] and lenarray[1] are both 0 if there are no matches.



 
 
BackUp LevelNext
 
 

allaire     AllaireDoc@allaire.com
    Copyright © 1998, Allaire Corporation. All rights reserved.