This does approximately the same as the regexp, but not quite. This will
only work if the space is there as a delimiter (and not a tab or a comma
without a space). If you add delimiters to the listGetAt, you don't put
the sentence correctly together. However, it will work in most cases.
Both our solutions have a problem if the sentence has two words and you
ask for three. In the list solution, just make sure that the
numberOfWords is less then ListLen(sentence," "). For the regexp try

Regexp = "(\w+\W+){0," & (i-1) & "}" & Regexp

> -----Original Message-----
> From: Jerry Johnson [mailto:[EMAIL PROTECTED]
> Sent: dinsdag 20 januari 2004 18:02
> To: CF-RegEx
> Subject: RE: Regex function help needed.
>
> Without Regex.
>
> <cfscript>
> function getwords(sentence,numberOfWords) { var str=""; for
> (i=1;i LTE numberOfWords;i=i+1) {
> str=listAppend(str,listGetAt(sentence,i," ")," ");
> }
> return(str);
> }
> </cfscript>
> <cfset s="Hotel Reservations Online. Discount Hotel
> Reservations in every city -  Orlando, Las Vegas, New York,
> London, Miami and more.">
>
> <cfset fragment=getWords(s,1)>
> <cfoutput>#fragment#</cfoutput><br>
>
> <cfset fragment=getWords(s,2)>
> <cfoutput>#fragment#</cfoutput><br>
> <cfset fragment=getWords(s,3)>
> <cfoutput>#fragment#</cfoutput><br>
> <cfset fragment=getWords(s,4)>
> <cfoutput>#fragment#</cfoutput><br>
> <cfset fragment=getWords(s,5)>
> <cfoutput>#fragment#</cfoutput><br>
>
>
> NOTE: you can replace the " " delimiter in the listGetAt
> function with a list of delimiters if you wish.
>
> Jerry Johnson
>
>
> >>> [EMAIL PROTECTED] 01/20/04 11:40AM >>>
> Then it all depends on what you define as a word.
>
> This should work (NOT TESTED)
>
> function GetWords(string,i){
> var Regexp = "\w+";
> var aTmp = "";
> var out = "";
> if(i GT 1) Regexp = "(\w+\W+){" & (i-1) & "}" & Regexp; aTmp
> = ReFindNoCase(Regexp,string,1,true);
> if(aTmp.pos[1]) out = Mid(string,aTmp.pos[1],aTmp.len[1]);
> return out;
> }
>
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

Reply via email to