試試這個:
$string = "Ing
Ing.
.Ing.
Xx Yy.
XX. YY.
XX.YY.";
if (preg_match('/^([A-Za-z]{1,}\.[ ]{0,})*/m', $string)) {
// Successful match
} else {
// Match attempt failed
}
結果:
正則表達式詳細:
^ Assert position at the beginning of a line (at beginning of the string or after a line break character)
( Match the regular expression below and capture its match into backreference number 1
[A-Za-z] Match a single character present in the list below
A character in the range between 「A」 and 「Z」
A character in the range between 「a」 and 「z」
{1,} Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\. Match the character 「.」 literally
[ ] Match the character 「 」
{0,} Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
正則表達式是不好的重複檢查。你唯一能做的就是匹配每個匹配的單詞,然後用代碼計算(普通)單詞並查看正則表達式匹配計數是否等於代碼匹配計數。祝你好運。 –