php - 匹配以以下开头的内容的正则表达式

标签 php regex

我正在尝试从这个字符串中获取一个匹配项

"Dial [Toll Free 1800 102 8880 ext: 246] to connect to the restaurant.  <a class='tooltip' title='Foodiebay has now introduced value added calling features through the website. You just need to dial this number and we ..."

我想检查变量是否以字符串 Dial 开头的地方

$a = 'Dial [Toll Free 1800 102 8880 ext: 246] to connect to the restaurant.  <a class='tooltip' title='Foodiebay has now introduced value added calling features through the website. You just need to dial this number and we';

preg_match('/[^Dial]/', $a, $matches);

最佳答案

去掉方括号:

/^Dial /

这与行首的字符串 "Dial " 匹配。

仅供引用:您的原始正则表达式是一个反转字符类 [^...],它匹配任何不在该类中的字符。在这种情况下,它将匹配任何不是“D”、“i”、“a”或“l”的字符。由于几乎每一行都至少有不是其中之一的字符,因此几乎每一行都会匹配。

关于php - 匹配以以下开头的内容的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4295249/

相关文章:

php - 我们在 PHP 的 cURL 中使用 CURLOPT_WRITEFUNCTION 有什么用?

regex - 我可以在 vim 中像这样替换多行吗?

javascript - 从扩展对象javascript创建 key 对对象

regex - htaccess 将 https 重定向到另一个 https

php - 如何将我自己的 Tripadvisor(或其他自定义图标)添加到 Divi Wordpress 主题的社交媒体图标?

php - 停止运行 PHP 服务器,命令行

php - return true 不返回或我的 else 声明不起作用?

javascript - 正则表达式删除最后一个数字后的字符串?

javascript - 如何对我们的电话号码进行正则表达式验证而不以 0 或 1 开头?

php - Laravel 中的日期格式查询