Here is my text:
TESTING TESTING test test test test test
I want the regex to return true (or a match) if more than 50% of the sentence is in capitals.
In this case, it would return false because only 14 letters of 20 are capitals.
In applescript, I’d do:
set a to characters of "abcdefghijklmnopqrstuvwxyz"
set ac to characters of "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set this_message to characters of "TEST TEST TEST TEST test test test test test test"
set x to 0 -- Counter
set y to 1
repeat with i from 1 to number of items in this_message
set this_item to item i of this_message
considering case
if this_item is not " " then
if this_item is in ac then
set x to x + 1
end if
end if
if this_item is in {" ", ",", ".", "-"} then
set y to y + 1
end if
end considering
end repeat
try
if (round (x / ((count this_message) - y)) * 100) > 50 then
return true
else
return false
end if
on error
return false
end try
Here is a PHP function that returns TRUE if a string contains more than half CAPs:
The above function compares the count of caps to the total length of the string (including whitespace and non-letters). If you want to compare to the number of non-whitespace chars, then the function is easily modified:
Here is a version that considers counts of whole words: