6 Replies - 5951 Views - Last Post: 07 July 2009 - 11:41 AM Rate Topic: -----

#1 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

reFindNoCase not returning expected subexpressions

Posted 21 November 2007 - 09:57 PM

Hi - First post here.

I'm trying to search a string for all values enclosed in a tilde ~. But its not working the way I expected. With the test string:


myString = this is a ~test~ ~some~ of the values are ~enclosed~ in a ~tilde~ symbol



I would have expected it to find these words:

test some enclosed tilde

But it only finds ~test~.

Is what I'm trying to do even possible? If it is, I would love it if someone could help me understand why the attached code doesn't work. Its bugging the heck out of me!


<cfset myString = "this is a ~test~ ~some~ of the values are ~enclosed~ in a ~tilde~ symbol">
<cfset result = reFindNoCase("~([^~]*)~", myString, 1, "true")>
<cfoutput>
myString = #myString#<br>
results:<br>
<cfloop from="1" to="#arrayLen(result.pos)#" index="i">
	#mid(myString, result.pos[i], result.len[i])#<br>
</cfloop>
</cfoutput>



Is This A Good Question/Topic? 1

Replies To: reFindNoCase not returning expected subexpressions

#2 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: reFindNoCase not returning expected subexpressions

Posted 29 November 2007 - 04:58 PM

Anybody?
Was This Post Helpful? 0
  • +
  • -

#3 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: reFindNoCase not returning expected subexpressions

Posted 10 December 2007 - 12:43 AM

Is this one a stumper, silly question or does this forum not get much traffic at all ?
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1956
  • View blog
  • Posts: 20,394
  • Joined: 17-March 01

Re: reFindNoCase not returning expected subexpressions

Posted 10 December 2007 - 07:38 AM

reFindNoCase will only find the first occurence of a string. Since reFindNoCase returns the position and length, you can do a while loop and keep calling reFindNoCase with the position+length+1 as the start position which will find the first instance, then start at the next character in the string after the first occurance and search again, it would continue to do this as long as reFindNoCase was returning true.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#5 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: reFindNoCase not returning expected subexpressions

Posted 10 December 2007 - 08:32 PM

View Postskyhawk133, on 10 Dec, 2007 - 07:38 AM, said:

reFindNoCase will only find the first occurence of a string. Since reFindNoCase returns the position and length, you can do a while loop and keep calling reFindNoCase with the position+length+1 as the start position which will find the first instance, then start at the next character in the string after the first occurance and search again, it would continue to do this as long as reFindNoCase was returning true.

Hope that helps.


Thanks. Doh. I can see I'm confusing find with REReplaceNoCase with its "all" scope. Of course its a totally different function ;)
Was This Post Helpful? 0
  • +
  • -

#6 paulfeakins  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 07-July 09

Re: reFindNoCase not returning expected subexpressions

Posted 07 July 2009 - 08:48 AM

View Postsansclue, on 10 Dec, 2007 - 07:32 PM, said:

I too was expecting REFindNoCase to return arrays of the length and position of each match. But I realised that if you have more than one subexpression in your RegEx it's those that are added to the array.

You can loop over your string though modifying the start position as per one of the posts above, something like this:

<cfset text="asdf=1234;qwerty=789">

<cfset stFind.len[1] = 0>
<cfset stFind.pos[1] = 1>
<cfset stReturn = StructNew()>

<cfloop condition="true">
<cfset stFind = REFindNoCase('(\w+?)="?([\w ]+?)[<";]', text, stFind.pos[1] + stFind.len[1], true)>
<cfif stFind.len[1] eq 0>
<cfbreak>
</cfif>
<cfset stReturn[Mid(text, stFind.pos[2], stFind.len[2])] = Mid(text, stFind.pos[3], stFind.len[3])>
</cfloop>

<cfdump var="#stReturn#&q


I too was expecting REFindNoCase to return arrays of the length and position of each match. But I realised that if you have more than one subexpression in your RegEx it's those that are added to the array.

You can loop over your string though modifying the start position as per one of the posts above, something like this:

<cfset text="asdf=1234;qwerty=789">

<cfset stFind.len[1] = 0>
<cfset stFind.pos[1] = 1>
<cfset stReturn = StructNew()>

<cfloop condition="true">
<cfset stFind = REFindNoCase('(\w+?)="?([\w ]+?)[<";]', text, stFind.pos[1] + stFind.len[1], true)>
<cfif stFind.len[1] eq 0>
<cfbreak>
</cfif>
<cfset stReturn[Mid(text, stFind.pos[2], stFind.len[2])] = Mid(text, stFind.pos[3], stFind.len[3])>
</cfloop>

<cfdump var="#stReturn#">
Was This Post Helpful? 1

#7 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: reFindNoCase not returning expected subexpressions

Posted 07 July 2009 - 11:41 AM

This post has been dead for nearly two years. Why are you replying to it now?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1