Shell脚本检查正确的案件判决

时间:2015-03-29 12:50:40

标签: shell sh

任何人都可以帮我正则表达式检查句子中的所有单词是否都是正确的情况,即以大写字母开头?

#!/bin/sh

SENTENCE="Blah blah blah"

PROPER_CASE_REGEX="\b^AZ*\b"  # <---- Here's where I need help please

if [[ ${SENTENCE} =~ $PROPER_CASE_REGEX ]];
then
    echo "Sentence IS proper case"
elif
then
    echo "Sentence is NOT proper case"
fi

1 个答案:

答案 0 :(得分:1)

您可以在BASH中使用此正则表达式(而不是在bourne shell中)来检查句子是否包含以大写字母开头的所有单词:

^([A-Z][[:alnum:]]*[[:blank:]]*)+$

<强>测试

re='^([A-Z][^[:blank:]]*[[:blank:]]*)+$'

sentence="Blah Blah Blah"    
[[ "$sentence" =~ $re ]] && echo "matched" || echo "no match"
matched

sentence="Blah Blah blah"
[[ "$sentence" =~ $re ]] && echo "matched" || echo "no match"
no match

RegEx Demo