| Contents > Developing ColdFusion MX Applications > Using Regular Expressions in Functions > Returning matched subexpressions > Specifying minimal matching |
|
|
|
|
||
The regular expression quantifiers ?, *, +, {min,} and {min,max} specify a minimum and/or maximum number of instances of a given expression to match. By default, ColdFusion locates the greatest number characters in the search string that match the regular expression. This behavior is called maximal matching.
For example, you use the regular expression "<b>(.*)</b>" to search the string "<b>one</b> <b>two</b>". The regular expression "<b>(.*)</b>", matches both of the following:
By default, ColdFusion always tries to match the regular expression to the largest string in the search string. The following code shows the results of this example:
<cfset sLenPos=REFind("<b>(.*)</b>", "<b>one</b> <b>two</b>", 1, "True")>
<cfoutput>
<cfdump var="#sLenPos#">
</cfoutput><br>
The following figure shows the output of the cfdump tag:

Thus, the starting position of the string is 1 and its length is 21, which corresponds to the largest of the two possible matches.
However, sometimes you might want to override this default behavior to find the shortest string that matches the regular expression. ColdFusion includes minimal-matching quantifiers that let you specify to match on the smallest string. The following table describes these expressions:
Expression |
Description |
|---|---|
*? |
minimal-matching version of * |
+? |
minimal-matching version of + |
?? |
minimal-matching version of ? |
{min,}? |
minimal-matching version of {min,} |
{min,max}? |
minimal-matching version of {min,max} |
{n}? |
(no different from {n}, supported for notational consistency) |
If you modify the previous example to use the minimal-matching syntax, the code is as follows:
<cfset sLenPos=REFind("<b>(.*?)</b>", "<b>one</b> <b>two</b>", 1, "True")>
<cfoutput>
<cfdump var="#sLenPos#">
</cfoutput><br>
The following figure shows the output of the cfdump tag:

Thus, the length of the string found by the regular expression is 10, corresponding to the string "<b>one</b>".
|
|
||
| Contents > Developing ColdFusion MX Applications > Using Regular Expressions in Functions > Returning matched subexpressions > Specifying minimal matching |
|
|
ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting
Version 6.1
Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/regexp20.htm