?= is used for a non capturing positive look ahead.

That means that it should follow a preceeding string and will normally be
the last part of a regex.

For example:

<cfset string = "streets">

<cfset regex = "t(?=s)">

<cfset matches = reFindNoCase(regex,string,1,true)>

<cfdump var="#matches#">

That will match the second "t" in streets, but not the first and it will not
capture the "s" that follows the "t"

If you want to put anything after the "?=" you need to include the contents
of the "?=" expression.

e.g. You could write this:


<cfset regex = "r(?=e)eet">

would match "reet"

<cfset regex = "r(?=e)et">

would fail because there isn't an occurrence of "ret" in the string.

What you want is "?:" which matches the string, doesn't capture it ,and
still allows you to have stuff before and after.

So your expression should look like this:

<cfset regex = ".*?(?:\[code\])(.*?)(?:\[\/code\]).*?">

Which will find one and only one occurrence of the expression if you use it
with reFindNoCase(). If there may be multiple occurrences of the tag pair in
the string you should use reReplaceNoCase().

Also, if you use ?: to discard part of the match you are losing information
that is useful when using reReplaceNoCase(). The information that is lost is
whether or not the matched string was inside a [code] block, or if it was at
the end of the string.

Here is an updated example of how to extract the information and use it
usefully using reReplaceNoCase() vs reFindNoCase():

<cfset string = "prefixing text [code] this is stuff [/code] mid text [code]
this is more stuff [/code] suffixing text">

<cfset tagName = "code">
<cfset regex = ".*?(\[" & tagName & "\])(.*?)(\[/" & tagName & "\]).*?">


<cfset match = reFindNoCase(regex,string,1,true)>
<cfoutput>
<div style="font-family: courier;">
String: "#string#"<br />
Regex: "#regex#"<br />
</div>
</cfoutput>

<br />
<strong>Using reFindNoCase()</strong>
<br />
<cftry>
        <cfoutput>
        #mid(string,match.pos[3],match.len[3])#<br />
        </cfoutput>
        <cfcatch>
            <cfoutput>Something went wrong!</cfoutput>
        </cfcatch>
</cftry>


<cfdump var="#match#">

<cfset newString =
reReplaceNoCase(string,regex,'\1#chr(3)#\2#chr(3)#\3#chr(3)#','all')>

<cfset matches = listToArray(newString,chr(3))>
<cfset usefulMatches = arrayNew(1)>

<br />
<strong>Using reReplaceNoCase()</strong>
<br />

<cftry>
        <cfloop from="2" to="#arrayLen(matches)#" index="i" step="3">
                <cfif matches[i-1] is "[code]" AND matches[i+1] IS
"[/code]">
                    <cfoutput>#matches[i]#<br /></cfoutput>
                    <cfset arrayAppend(usefulMatches,matches[i])>
                </cfif>
        </cfloop>

        <cfcatch>
            <cfoutput>Something went wrong!</cfoutput>
        </cfcatch>
</cftry>
<br />
<br />
<cfdump var="#matches#" label="All Matches">
<cfdump var="#usefulMatches#" label="Useful Matches">

Spike




--------------------------------------------
Stephen Milligan
Code poet for hire
http://www.spike.org.uk

Do you cfeclipse? http://cfeclipse.tigris.org 


 

>-----Original Message-----
>From: [EMAIL PROTECTED] 
>[mailto:[EMAIL PROTECTED] On Behalf Of 
>Steve Onnis
>Sent: Sunday, May 23, 2004 8:57 AM
>To: CFAussie Mailing List
>Subject: [cfaussie] Re: Regular Expression making my hair fall out~!
>
>Spike
>
>Thanks for the reply
>
>if the docs say that (?=...) will check for it but not include 
>it in the results, then 
>REFindNoCase("(?=(\[code\]))(.*?)(?=(\[\/code\]))",tmpContent,1,true);
>should do exactly that.
>
>Is this an issue with the reg exp engine in cf or the reg exp its self?
>
>thats what I am wanting to get to the bottom of
>
>Steve
>
>
>-----Original Message-----
>From: [EMAIL PROTECTED]
>[mailto:[EMAIL PROTECTED] Behalf Of 
>Stephen Milligan
>Sent: Monday, May 24, 2004 2:29 AM
>To: CFAussie Mailing List
>Subject: [cfaussie] Re: Regular Expression making my hair fall out~!
>
>
>You will find as you use regular expressions more that it is 
>often easier to replace the contents of a string than to use 
>find and then extract it.
>
>eg.
>
><cfset string = "bla bla bla [code] this is stuff [/code] bla 
>bla bla [code] this is more stuff [/code]">
>
><cfset tagName = "code">
>
><cfset regex = ".*?(\[" & tagName & "\])(.*?)(\[/" & tagName & "\])">
>
><cfset match = reFindNoCase(regex,string,1,true)>
>
><cfoutput>
><div style="font-family: courier;">
>String: "#string#"<br />
>Regex: "#regex#"<br />
></div>
></cfoutput>
>
><br />
><strong>Using reFindNoCase()</strong>
><br />
><cftry>
>       <cfoutput>
>       #mid(string,match.pos[2],match.len[2])#<br />
>       #mid(string,match.pos[3],match.len[3])#<br />
>       #mid(string,match.pos[4],match.len[4])#<br />
>       </cfoutput>
>
>       <cfcatch>
>           <cfoutput>Done!</cfoutput>
>       </cfcatch>
></cftry>
>
><cfset newString = reReplaceNoCase(string,regex,'\1<br />\2<br 
>/>\3<br />','all')> <br /> <strong>Using 
>reReplaceNoCase()</strong> <br /> <cfoutput>#newString#</cfoutput>
>
>Regards
>
>Spike
>
>--------------------------------------------
>Stephen Milligan
>Code poet for hire
>http://www.spike.org.uk
>
>Do you cfeclipse? http://cfeclipse.tigris.org
>
>
>
>
>>-----Original Message-----
>>From: [EMAIL PROTECTED]
>>[mailto:[EMAIL PROTECTED] On Behalf Of Steve 
>>Onnis
>>Sent: Saturday, May 22, 2004 11:07 PM
>>To: CFAussie Mailing List
>>Subject: [cfaussie] Re: Regular Expression making my hair fall out~!
>>
>>Graham
>>
>>Thanks for your effort, but as my post said, i am trying to 
>extract it 
>>from a string, not replace it, hence I am using REFind, not REReplace.
>>
>>
>>Steve
>>
>>-----Original Message-----
>>From: [EMAIL PROTECTED]
>>[mailto:[EMAIL PROTECTED] Behalf Of Graham 
>>Cook
>>Sent: Sunday, May 23, 2004 11:27 AM
>>To: CFAussie Mailing List
>>Subject: [cfaussie] Re: Regular Expression making my hair fall out~!
>>
>>
>>Here's your solution:
>>
>>#REReplaceNoCase("[code] This is the code to extract 
>[/code]","\[code\]
>>([[:print:]]*) \[/code\]"," \1","ALL")#
>>
>>---
>>You are currently subscribed to cfaussie as:
>>[EMAIL PROTECTED] To unsubscribe send a blank email to 
>>[EMAIL PROTECTED]
>>
>>MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia 
>>http://www.mxdu.com/ + 24-25 February, 2004
>>
>>
>>---
>>You are currently subscribed to cfaussie as:
>>[EMAIL PROTECTED] To unsubscribe send a blank email to 
>>[EMAIL PROTECTED]
>>
>>MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia 
>>http://www.mxdu.com/ + 24-25 February, 2004
>
>
>---
>You are currently subscribed to cfaussie as: 
>[EMAIL PROTECTED] To unsubscribe send a blank email to 
>[EMAIL PROTECTED]
>
>MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia 
>http://www.mxdu.com/ + 24-25 February, 2004
>
>
>---
>You are currently subscribed to cfaussie as: 
>[EMAIL PROTECTED] To unsubscribe send a blank email to 
>[EMAIL PROTECTED]
>
>MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia 
>http://www.mxdu.com/ + 24-25 February, 2004


---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]

MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia
http://www.mxdu.com/ + 24-25 February, 2004

Reply via email to