House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home / Groups / Regular Expressions (RegEx)

Count word in one line and <enter>

Author:
Peter Boughton
07/08/2009 08:42 AM

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.


Search regex

February 12, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29