Simple script to count characters and words in selection

Get help with using AutoHotkey and its commands and hotkeys
surfmadpig
Posts: 1
Joined: 02 Apr 2019, 07:22

Simple script to count characters and words in selection

02 Apr 2019, 07:46

How can I get a MsgBox displaying the number of words, number of characters (including spaces) and number of characters (not counting spaces) in a selection?

Apologies if this is a pretty basic question. I haven't managed to find an AHK script that does all of the above.
User avatar
sinkfaze
Posts: 614
Joined: 01 Oct 2013, 08:01

Re: Simple script to count characters and words in selection

02 Apr 2019, 08:26

Character count:

Code: Select all

t=This isn't a test.
MsgBox %	StrLen(t)
Character count excluding spaces (this does not include `r`n or other vertical whiteapce):

Code: Select all

t=This isn't a test.
MsgBox %	StrLen(StrReplace(t," "))
Word count:

Code: Select all

t=This isn't a test. This is just an exercise. Promise.
RegExReplace(t,"\b[\w\-\']+\b","",c)
MsgBox %	c
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simple script to count characters and words in selection

02 Apr 2019, 16:27

- Here's an attempt at some word count code.

Code: Select all

q:: ;word count
vText := "This isn't a test. This is just an exercise. Promise."
vLen1 := StrLen(vText)
vLen2 := StrLen(RegExReplace(vText, "([ `t`r`n])((?1)*)", "$2"))

;word count = gaps + 1:
;vWordCount := vLen1 - vLen2 + 1

;word count = gaps + 1 (if the text is just a gap with no words, word count = 0):
vWordCount := vLen1 - vLen2 + !RegExMatch(vText, "^[ `t`r`n]*$") 
;vWordCount := vLen1 - vLen2 + !(Trim(vText, " `t`r`n") = "") ;an alternative

MsgBox, % vWordCount
return
- The idea is this: if you see a stream of spaces/tabs/enter characters, the first character of that stream is removed.
- The number of characters removed = the number of gaps between words.
- The number of gaps between words + 1 = the number of words.
- I also consider that if the text only contains spaces/tabs/enter characters, there are zero words.

- Here are some tests:

Code: Select all

q:: ;RegEx - remove first character in pattern (handle repeated subpatterns)
;(subpatterns as 'variables') (backreferences) (capturing groups) (word count)
vText := "aaabbbcccaaabbbcccaaabbbccc"
MsgBox, % RegExReplace(vText, "(b)(b*)", "$2")
MsgBox, % RegExReplace(vText, "(?<!b)b(?=b+)")
MsgBox, % RegExReplace(vText, "(b)((?1)*)", "$2")
MsgBox, % RegExReplace(vText, "(?P<p1>b)((?P=p1)*)", "$2")
return

;source: http://www.pcre.org/pcre.txt
;(?n)            call subpattern by absolute number
;(?P<name>...)   named capturing group (Python)
;(?P=name)       reference by name (Python)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], BushMange, CEA6597, LiangShuii, VACO BenQ, w0z and 201 guests