|
Mailing Lists
|
Home / Groups / Regular Expressions (RegEx)
Count word in one line and <enter>Thank you for replying, actually, I want to count how many line has that word. It does not a problem if the word is repeated or not as long as the word is found in the line. For another example. I have a 5 lines of sentences. <text> I have a green apple. I have two apple. Green apple and red apple. I have an orange. My brother has red apple and green apple. I have an orange but my friend give me red apple. I do have orange and two books. The two books are given by my friend. </text> In this case, I have 4 lines of words. So If I want to count how many line contains 'apple'; I will receive 4 lines. If I want to count how many line contains 'red apple'; i will receive 2 lines/ If I want to count how many line contains 'two'; I will receive 2 lines. If I want to count how many line contains 'two' or 'green'; i will receive 4 lines If I want to count how many line contains 'red apple' or 'green apple'; I will receive 4 lines. >Huh? You seem to be asking two questions? > >Do you want the number of words in the text (12), or the number of >non-empty lines (2)? > > >With the behaviour of CF's List functions to ignore empty elements, >you don't need regex, you can simply do: > >NumberOfLines = ListLen( Text , Chr(10) ) > > >For the number of words, it is a little more complex - you need to >treat any whitespace as a delimiter: > >NumberOfWords = ListLen( Text , ' ' & Chr(13) & Chr(10) & Chr(9) ) > >This is not perfect (it will treat hyphens, ampersands, etc as words). > > >If you do want to get actual words only (i.e. , you could try >something like this: > ><cfset Words = rematch(String,"\w[\w'-]+") /> ><cfoutput>#ArrayLen(Words)# words</cfoutput> > >The \w means "word character" whilst the [\w'-] means "word character >or apostrophe or dash", so it will match things like "half-past" and >"o'clock" as single words, but will treat "like - this" or "this & >that" as just 2 words. > > >Anyway, if this isn't what you're after, you'll need to clarify your question. |
February 12, 2012
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||