Android Question RegEx for word counting

peacemaker

Expert
Licensed User
Longtime User
HI, All

If we have a big string, how to count the words qty ?
Words delimiters usually are ".,;:!?#()\|/" & the space.

If national letters [А-Яа-яЁё]
 
Last edited:

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
B4X:
\w+(-\w+)*

http://bfy.tw/K3qL ;)

- edited to correct for java regex and add B4A example:

B4X:
    Dim poMatch As Matcher
    Dim piCount As Int
    poMatch = Regex.Matcher("\w+(-\w+)*",  "There are seven words in this sentence.")
    Log(poMatch.GroupCount)
    Do While poMatch.Find
        Log("Found " & poMatch.Match)
        piCount = piCount + 1
    Loop
    Log(piCount & " words.")

If you need non-standard letters you can be specific and add your "national letters" into the match instead of the default "w" indicator using this syntax:
B4X:
[a-zA-Z0-9]+([-_][a-zA-Z0-9]+)*
 
Last edited:
Upvote 0
Top