這裏是我的文字:正則表達式找到不必要的大寫單詞
TESTING TESTING test test test test test
我想正則表達式返回true(或匹配)如果句子的50%以上是大寫字母。
在這種情況下,它將返回false,因爲只有14個字母的20是首都。
在AppleScript的,我會做:
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
我不認爲正則表達式是這項工作的正確工具。在Perl中,我會使用'tr //';你將使用哪種語言? – Flimzy
我將要使用php。但是如果另一種語言對工作更好,我會使用它。 – alexy13