home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / CHIPCD_02_2002.iso / Internet / Macromedia ColdFusion Server 5 / coldfusion-50-win-us.exe / data1.cab / Examples / CFDOCS / snippets / refindnocase.cfm < prev    next >
Encoding:
Text File  |  2001-06-13  |  3.8 KB  |  90 lines

  1. <!--- This example shows the use of REFindNoCase --->
  2.  
  3. <HTML>
  4.  
  5. <HEAD>
  6. <TITLE>
  7. REFindNoCase Example
  8. </TITLE>
  9. </HEAD>
  10.  
  11. <BASEFONT FACE="Arial, Helvetica" SIZE=2>
  12. <BODY  bgcolor="#FFFFD5">
  13.  
  14. <H3>REFindNoCase Example</H3>
  15.  
  16. <P>This example demonstrates the use of the REFindNoCase function with and without the <i>returnsubexpressions</i> parameter set to True.</P> 
  17.  
  18. If you do not use the <i>returnsubexpressions</i> parameter, REFindNoCase returns the position of the first occurrence of a regular expression in a string starting from the specified position.  Returns 0 if no occurrences are found.
  19. </P>
  20.  
  21. <P>REFindNoCase("a+c+", "abcaaccdd"):    
  22. <CFOUTPUT>#REFindNoCase("a+c+", "abcaaccdd")#</CFOUTPUT></P>
  23. <P>REFindNoCase("a+c*", "abcaaccdd"):    
  24. <CFOUTPUT>#REFindNoCase("a+c*", "abcaaccdd")#</CFOUTPUT></P>
  25. <P>REFindNoCase("[[:alpha:]]+", "abcaacCDD"):    
  26. <CFOUTPUT>#REFindNoCase("[[:alpha:]]+", "abcaacCDD")#</CFOUTPUT></P>
  27. <P>REFindNoCase("[\?&]rep=", "report.cfm?rep=1234&u=5"):
  28. <CFOUTPUT>#REFindNoCase("[\?&]rep=", "report.cfm?rep=1234&u=5")#</CFOUTPUT>
  29. </P>
  30. <!--- Use REFindNoCase with 1 as startPos and returnMatchedSubexpressions = 
  31.     TRUE --->
  32. <HR size="2" color="#0000A0">
  33.  
  34. <P>If you do use the <i>returnssubexpression</i> parameter, REFindNoCase 
  35. returns the position and length of the first occurrence of a regular expression in a string 
  36. starting from the specified position. The position and length variables are stored in a structure. 
  37. In order to access the position and length information, you must use the keys 
  38. <i>pos</i> and <i>len</i>, respectively.</P>
  39.  
  40. <CFSET teststring ="The cat in the hat hat came back!">
  41. <P>The string in which the function is to search is: <CFOUTPUT><b>#teststring#</b></CFOUTPUT>.</P>
  42. <P>The first call to REFindNoCase to search this string is: 
  43. <b>REFindNoCase("[[:alpha:]]+",testString,1,"TRUE")</b></P>
  44. <P>This function returns a structure that contains two arrays: pos and len.</P>
  45. <P>In order to create this structure you can use a CFSET statement, for example:</P>
  46. <CFSET st = REFindNoCase("[[:alpha:]]+",testString,1,"TRUE")>
  47. <CFSET st = REFindNoCase("[[:alpha:]]+",testString,1,"TRUE")>
  48. <P>
  49.     <CFOUTPUT>
  50.     The number of elements in each array: #ArrayLen(st.pos)#.
  51.     </CFOUTPUT>
  52. </P>
  53. <P><b>The number of elements in the pos and len arrays will always be one if you 
  54. do not use parentheses to denote subexpressions in the regular expression.</b></P>
  55. <P>The value of st.pos[1] is: <CFOUTPUT>#st.pos[1]#.</CFOUTPUT></P>
  56. <P>The value of st.len[1] is: <CFOUTPUT>#st.len[1]#.</CFOUTPUT></P>
  57. <P>
  58.     <CFOUTPUT>
  59.     Substring is <b>[#Mid(testString,st.pos[1],st.len[1])#]</B>
  60.     </CFOUTPUT>
  61. </P>
  62.  
  63. <HR size="2" color="#0000A0">
  64.  
  65. <P>However, if you use parentheses to denote subexpressions in the regular 
  66. expression, you will find that the first element contains the position and length 
  67. of the first instance of the whole expression. The position and length of the 
  68. first instance of each subexpression within will be included in additional array 
  69. elements.</P>
  70.  
  71. <P>For example: <CFSET st1 = REFindNoCase("([[:alpha:]]+)[ ]+(\1)",testString,1,"TRUE")></P>
  72.  
  73. <CFSET st1 = REFindNoCase("([[:alpha:]]+)[ ]+(\1)",testString,1,"TRUE")>
  74.  
  75. <P>The number of elements in each array is <CFOUTPUT>#ArrayLen(st1.pos)#</CFOUTPUT>.</P>
  76.  
  77. <P>First whole expression match; position is 
  78. <CFOUTPUT>
  79. #st1.pos[1]#; length is #st1.len[1]#; 
  80. whole expression match is <B>[#Mid(testString,st1.pos[1],st1.len[1])#]</B>
  81. </CFOUTPUT></P>
  82.  
  83. <P>Subsequent elements of the arrays provide the position and length of the first instance of each parenthesized subexpression therein.</P>
  84.  <CFLOOP index="i" from="2" to="#ArrayLen(st1.pos)#">
  85.     <p><CFOUTPUT>Position is #st1.pos[i]#; Length is #st1.len[i]#; Substring is <B>[#Mid(testString,st1.pos[i],st1.len[i])#]</B></CFOUTPUT></p>
  86. </CFLOOP><BR>   
  87.  
  88. </BODY>
  89.  
  90. </HTML>