2011-12-03 54 views
0

這裏是我的文字:正則表達式找到不必要的大寫單詞

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 
+0

我不認爲正則表達式是這項工作的正確工具。在Perl中,我會使用'tr //';你將使用哪種語言? – Flimzy

+0

我將要使用php。但是如果另一種語言對工作更好,我會使用它。 – alexy13

回答

2

這裏是一個PHP函數如果字符串包含超過一半的CAP返回TRUE:

// Test if more than half of string consists of CAPs. 
function isMostlyCaps($text) { 
    $len = strlen($text); 
    if ($len) { // Check if string has zero length. 
     $capscnt = preg_match_all('/[A-Z]/', $text, $matches); 
     if ($capscnt/$len > 0.5) return TRUE; 
    } 
    return FALSE; 
} 

上述功能帽的計數進行比較,以字符串的總長度(包括空格和非字母)。如果你要比較非空格字符的數量,那麼該功能方便地進行修改:

// Test if more than half of non-whitespace chars in string are CAPs. 
function isMostlyCaps($text) { 
    $len = preg_match_all('/\S/', $text, $matches); 
    if ($len) { // Check if string has zero length. 
     $capscnt = preg_match_all('/[A-Z]/', $text, $matches); 
     if ($capscnt/$len > 0.5) return TRUE; 
    } 
    return FALSE; 
} 

這裏是考慮了整個單詞計數版本:

// Test if more than half of "words" in string are all CAPs. 
function isMostlyCapWords($text) { 
    // For our purpose a "word" is a sequence of non-whitespace chars. 
    $wordcnt = preg_match_all('/\S+/', $text, $matches); 
    if ($wordcnt) { // Check if string has no words. 
     $capscnt = preg_match_all('/\b[A-Z]+\b/', $text, $matches); 
     if ($capscnt/$wordcnt > 0.5) return TRUE; 
    } 
    return FALSE; 
} 
+0

謝謝你的答案 – alexy13

1

在Perl中:

sub mostly_caps { 
    my $string = shift; 
    my $upper = $string =~ tr/A-Z//; 
    my $lower = $string =~ tr/a-z//; 
    return $upper >= $lower; 
} 

而對於加分,一個版本,接受一個任意百分比作爲參數:

sub caps_pct { 
    my ($string, $pct) = @_; 
    my $upper = $string =~ tr/A-Z//; 
    my $lower = $string =~ tr/a-z//; 
    return ($upper/($upper+$lower) >= $pct/100; 
} 

它應該很容易適應PHP或任何其他語言。

+0

+1,雖然這隻適用於50%:) – FailedDev

+0

@FailedDev:這是要求,對吧?我想另一個公式可以輕鬆解決任意的百分比。 – Flimzy

+0

嗯,是的,雖然更一般的解決方案會更好:) – FailedDev

相關問題