PHP 中的预匹配检查


Preg Match checking in PHP

我有这样的模式'D34566'。 我如何检查给定字符串中包含的此模式。

String =  D34566-Test Case.
Pattern = D followed by 5 digits.

即以 D 开头并后跟 5 位数字的单词。

preg_match("/^D'd{5}/", $string)

您可以尝试以下模式来检查要在字符串中的任何位置找到的单词:

if (preg_match('/'bD'd{5}'b/', $string)) {
    // OK
}

像这样:

preg_match('/^(D'd{5})/', String);