|
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
More RegEx fun (Negative lookaheads)
I wanted to see if I could use a negative look ahead on this block of code to make sure if there's a var rbo=""; in the code, there must be a rbo = closeObj(); somewhere after it. Like so...Will Tomlinson 07/03/08 02:03 P I didn't phrase that right. I want to check and make sure this is located after the var rbo line. I guess I could do one at a time.Will Tomlinson 07/03/08 02:05 P Dangit. I can't make this flag it. It LOOKS like it'd work.Will Tomlinson 07/03/08 02:42 P Will,Nathan Strutz 07/03/08 03:03 P >Will,Will Tomlinson 07/03/08 03:39 P >Will,Will Tomlinson 07/03/08 04:27 P Wowee... in CF8, this works with a lookahead.Will Tomlinson 07/03/08 04:47 P > Lookaround doesn't work with ColdFusion's regex engine. Instead, useMassimo Foti 07/03/08 06:05 P If you are using any Cold Fusion MX version you can use the underlying regexPatrick Santora 07/03/08 09:17 P I wanted to see if I could use a negative look ahead on this block of code to make sure if there's a var rbo=""; in the code, there must be a rbo = closeObj(); somewhere after it. Like so... <cfset str = ' <cfscript> var rbo = ""; var someVar = "Will"; var someOtherVar = "Whatever"; Whateverelse logic here... rbo = closeObj(); rbo = ""; '> I played around with a negative lookahead and it looks like a powerful regex tool... I just can't make it work. I gave up at the \2 <cfset re = '(.+)(var\srbo\s=\s"";)(\2)'> <cfoutput>#reFindNoCase(re, str)#</cfoutput> I'm trying to capture the whole chunk, capture the rbo line only, then lookahead to see if it's located in the rest of the whole chunk. Make sense? Thanks, Will I didn't phrase that right. I want to check and make sure this is located after the var rbo line. I guess I could do one at a time. rbo = closeObj(); rbo = ""; Dangit. I can't make this flag it. It LOOKS like it'd work. Find the first instance of the rbo call, then make sure it's closed AFTERwards. <cfset str = ' <cfscript> var rbo = ""; var someVar = "Will"; var someOtherVar = "Whatever"; Whateverelse logic here... rbo = closestObj(); rbo = ""; </cfscript> '> <cfset re = 'var\s(rbo\s=\s"";)(rbo\s=\scloseObj\(\);)\1(?!\2)'> <cfoutput>#reFindNoCase(re, str)#</cfoutput> Will, Lookaround doesn't work with ColdFusion's regex engine. Instead, use Java's: #str.matches(re)# (returns true/false) also look into replaceFirst, replaceAll and split. Ehh, I'd link my blog where I have a couple entries and a presentation on it, but I've been having DNS issues for a few days, and it looks like they'll continue through the next week... Instead, here's a link to java.lang.string - if you can do it in Java, you can do it in CF! http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html nathan strutz http://www.dopefly.com/ On Thu, Jul 3, 2008 at 11:38 AM, Will Tomlinson <will@wtomlinson.com> wrote: ----- Excess quoted text cut - see Original Post for more ----- ----- Excess quoted text cut - see Original Post for more ----- Thanks dude! I'll check it out. Will >Will, > >Lookaround doesn't work with ColdFusion's regex engine. Instead, use Java's: > >#str.matches(re)# (returns true/false) > >also look into replaceFirst, replaceAll and split. Check this out too: http://www.bennadel.com/blog/769-Learning-ColdFusion-8-REMatch-For-Regular-Expression-Matching.htm But, until now, ColdFusion has been extremely weak when it came to retrieving substrings from a chunk of text using regular expressions. Thankfully, ColdFusion 8 has introduced REMatch() and REMatchNoCase(). REMatch() searches through a string using a regular expression and returns all matching patterns in an array. Wowee... in CF8, this works with a lookahead. <cfset str = ' <cfscript> var rbo = ""; var someVar = "Will"; var someOtherVar = "Whatever"; Whateverelse logic here... rbo = closeggObj(); rbo = ""; </cfscript> '> <cfset re = '(?!rbo\s=\s"";)\nvar\ssomeVar'> <cfset result = REMatchNoCase(re, str)> <cfdump var="#result#"> Returns an array: 1 var someVar > Lookaround doesn't work with ColdFusion's regex engine. Instead, use > Java's: As far as I remember lookaheads work, lookbehind don't. Please correct me if I am wrong. Will, if you want to use Java RegExp, you can try this CFC: http://www.massimocorner.com/coldfusion/cfc/tmt_java_regexp.zip ---------------------------- Massimo Foti, web-programmer for hire Tools for ColdFusion, JavaScript and Dreamweaver: http://www.massimocorner.com ---------------------------- If you are using any Cold Fusion MX version you can use the underlying regex engine. I came into this thread late, but if this helps you then great. Take a look at the below blog to see what mean. This works with Cold Fusion version 6 to current. http://patweb99.avatu.com/post.cfm/how-to-access-underlying-java-regular-expression-functionality-in-coldfusion-to-get-around-re-function-limitations On Thu, Jul 3, 2008 at 3:01 PM, Massimo Foti <massimo@massimocorner.com> wrote: ----- Excess quoted text cut - see Original Post for more -----
|
Mailing Lists
|
Latest Fusion Authority Articles
|
||||||