refindNoCase Example

This example demonstrates the use of the refindNoCase function with and without the returnsubexpressions parameter set to True.

If you do not use the returnsubexpressions 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.

refindNoCase("a+c+", "abcaaccdd"): #refindNoCase("a+c+", "abcaaccdd")#

refindNoCase("a+c*", "abcaaccdd"): #refindNoCase("a+c*", "abcaaccdd")#

refindNoCase("[[:alpha:]]+", "abcaacCDD"): #refindNoCase("[[:alpha:]]+", "abcaacCDD")#

refindNoCase("[\?&]rep = ", "report.cfm?rep = 1234&u = 5"): #refindNoCase("[\?&]rep = ", "report.cfm?rep = 1234&u = 5")#


If you do use the returnssubexpression parameter, refindNoCase returns the position and length of the first occurrence of a regular expression in a string starting from the specified position. The position and length variables are stored in a structure. To access the position and length information, you must use the keys pos and len, respectively.

The string in which the function is to search is: #teststring#.

The first call to refindNoCase to search this string is: refindNoCase("[[:alpha:]]+",testString,1,"TRUE")

This function returns a structure that contains two arrays: pos and len.

To create this structure you can use a CFSET statement, for example:

<CFSET st = refindNoCase("[[:alpha:]]+",testString,1,"TRUE")>

The number of elements in each array: #ArrayLen(st.pos)#.

The number of elements in the pos and len arrays will always be one if you do not use parentheses to denote subexpressions in the regular expression.

The value of st.pos[1] is: #st.pos[1]#.

The value of st.len[1] is: #st.len[1]#.

Substring is [#Mid(testString,st.pos[1],st.len[1])#]


However, if you use parentheses to denote subexpressions in the regular expression, you will find that the first element contains the position and length of the first instance of the whole expression. The position and length of the first instance of each subexpression within will be included in additional array elements.

For example: <CFSET st1 = refindNoCase("([[:alpha:]]+)[ ]+(\1)",testString,1,"TRUE")>

The number of elements in each array is #ArrayLen(st1.pos)#.

First whole expression match; position is #st1.pos[1]#; length is #st1.len[1]#; whole expression match is [#Mid(testString,st1.pos[1],st1.len[1])#]

Subsequent elements of the arrays provide the position and length of the first instance of each parenthesized subexpression therein.

Position is #st1.pos[i]#; Length is #st1.len[i]#; Substring is [#Mid(testString,st1.pos[i],st1.len[i])#]


[allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => [htmlstate] => on_nl2br [postusername] => The ScareCrow [ip] => info@krcaldwell [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 2 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> FIND Amount in String - Macromedia ColdFusion

FIND Amount in String - Macromedia ColdFusion

I have this: <cfset TempVar = "This is a test string to test the code"> I need to find the amount of times the word "test" appears in the above string. How can i do this? Thank you!...

  1. #1

    Default FIND Amount in String

    I have this:

    <cfset TempVar = "This is a test string to test the code">
    I need to find the amount of times the word "test" appears in the above string. How can i do this?

    Thank you!
    Vbprog40 Guest

  2. #2

    Default Re: FIND Amount in String

    use refind.

    I have attached the help doc from studio (just copy, paste and run), the last
    paragraph is what you need to do.

    Ken

    <!--- This example shows the use of refindNoCase --->
    <html>

    <head>
    <title>
    refindNoCase Example
    </title>
    </head>

    <body>

    <H3>refindNoCase Example</H3>

    <P>This example demonstrates the use of the refindNoCase function with
    and without the <i>returnsubexpressions</i> parameter set to True.</P>

    <P>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.
    </P>

    <P>refindNoCase("a+c+", "abcaaccdd"):
    <cfoutput>#refindNoCase("a+c+", "abcaaccdd")#</cfoutput></P>
    <P>refindNoCase("a+c*", "abcaaccdd"):
    <cfoutput>#refindNoCase("a+c*", "abcaaccdd")#</cfoutput></P>
    <P>refindNoCase("[[:alpha:]]+", "abcaacCDD"):
    <cfoutput>#refindNoCase("[[:alpha:]]+", "abcaacCDD")#</cfoutput></P>
    <P>refindNoCase("[\?&]rep = ", "report.cfm?rep = 1234&u = 5"):
    <cfoutput>#refindNoCase("[\?&]rep = ", "report.cfm?rep = 1234&u =
    5")#</cfoutput>
    </P>
    <!--- Set startPos to one; returnMatchedSubexpressions = TRUE --->
    <hr size = "2" color = "#0000A0">

    <P>If you do use the <i>returnssubexpression</i> parameter,
    refindNoCase returns the position and length of the first
    occurrence of a regular expression in a string starting from
    the specified position. The position and length variables are
    stored in a structure. To access the position and
    length information, you must use the keys <i>pos</i> and
    <i>len</i>, respectively.</P>

    <cfset teststring = "The cat in the hat hat came back!">
    <P>The string in which the function is to search is:
    <cfoutput><b>#teststring#</b></cfoutput>.</P>
    <P>The first call to refindNoCase to search this string is:
    <b>refindNoCase("[[:alpha:]]+",testString,1,"TRUE")</b></P>
    <P>This function returns a structure that contains two arrays:
    pos and len.</P>
    <P>To create this structure you can use a CFSET statement,
    for example:</P>
    &lt;CFSET st = refindNoCase("[[:alpha:]]+",testString,1,"TRUE")&gt;
    <cfset st = refindNoCase("[[:alpha:]]+",testString,1,"TRUE")>
    <P>
    <cfoutput>
    The number of elements in each array: #ArrayLen(st.pos)#.
    </cfoutput>
    </P>
    <P><b>The number of elements in the pos and len arrays will always be
    one if you do not use parentheses to denote subexpressions in
    the regular expression.</b></P>
    <P>The value of st.pos[1] is: <cfoutput>#st.pos[1]#.</cfoutput></P>
    <P>The value of st.len[1] is: <cfoutput>#st.len[1]#.</cfoutput></P>
    <P>
    <cfoutput>
    Substring is <b>[#Mid(testString,st.pos[1],st.len[1])#]</B>
    </cfoutput>
    </P>
    <hr size = "2" color = "#0000A0">

    <P>However, if you use parentheses to denote subexpressions in the
    regular expression, you will find that the first element
    contains the position and length of the first instance of the
    whole expression. The position and length of the first instance
    of each subexpression within will be included in additional
    array elements.</P>

    <P>For example:
    &lt;CFSET st1 = refindNoCase("([[:alpha:]]+)[
    ]+(\1)",testString,1,"TRUE")&gt;</P>

    <cfset st1 = refindNoCase("([[:alpha:]]+)[ ]+(\1)",testString,1,"TRUE")>

    <P>The number of elements in each array is
    <cfoutput>#ArrayLen(st1.pos)#</cfoutput>.</P>

    <P>First whole expression match; position is
    <cfoutput>
    #st1.pos[1]#; length is #st1.len[1]#;
    whole expression match is
    <B>[#Mid(testString,st1.pos[1],st1.len[1])#]</B>
    </cfoutput></P>

    <P>Subsequent elements of the arrays provide the position and length of
    the first instance of each parenthesized subexpression therein.</P>
    <CFLOOP index = "i" from = "2" to = "#ArrayLen(st1.pos)#">
    <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>
    </CFLOOP><BR>
    </body>
    </html>

    The ScareCrow Guest

  3. #3

    Default Re: FIND Amount in String

    Whats isa this: <cfset st = refindNoCase("[[:alpha:]]+",testString,1,"TRUE")>


    How do i get it what to look for.. with the above code it along looks for the
    first repeater... not all of them.

    So if you have: <cfset teststring = "The two two cat in the back! four four">

    Vbprog40 Guest

  4. #4

    Default Re: FIND Amount in String

    It's a regular expression.

    I'm crap at these so I can't help there.

    Ken
    The ScareCrow Guest

  5. #5

    Default Re: FIND Amount in String

    You might try to compare each word in your sentence to the word defined by you
    to see how many times it repeated. I attached a piece code that might be use

    Berk

    <cfloop index="i" from="1" to"15">
    <cfset word = GetToken(#sentence#, i, ' ')>
    <cfif word EQ user_word>
    <cfset count = count +1>
    </cfif>
    </cfloop>

    BerkTheTurk Guest

  6. #6

    Default Re: FIND Amount in String

    Another way:

    <cfset TempVar = "This is a test string to test the code">
    <cfset x = 1>
    <cfset cntr = 0>
    <cfloop condition="#FindNoCase('test',tempvar,x)#">
    <cfset cntr = cntr + 1>
    <cfset x = FindNoCase('test',tempvar,x) + 1>
    </cfloop>
    <cfoutput>"test" appears #cntr# times</cfoutput>

    OldCFer Guest

Similar Threads

  1. Find and cut string
    By Ola Ekedahl in forum PHP Development
    Replies: 1
    Last Post: November 23rd, 01:48 PM
  2. [PHP] find string
    By Carl Furst in forum PHP Development
    Replies: 3
    Last Post: August 11th, 12:19 AM
  3. find in string
    By Bite My Bubbles in forum ASP
    Replies: 4
    Last Post: August 9th, 08:02 PM
  4. find string between files
    By e.group in forum Sun Solaris
    Replies: 1
    Last Post: August 3rd, 11:17 AM
  5. String Find Function
    By Mike Davies in forum Microsoft SQL / MS SQL Server
    Replies: 3
    Last Post: July 1st, 01:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •