PHP 和正则表达式 : search for word in string

标签 php mysql regex

如何使用正则表达式在字符串中找到单词或不在该字符串中找到单词

字符串是Mysql查询

SELECT 
  id,
  name,
  desc,
  (SELECT id as lid FROM comments WHERE lid = id order by id desc limit 1) as lid_com
 FROM posts limit 3

这里我想在字符串的最后搜索limit

字符串可能是

  limit 3
  limit 3, 3
  limit 3 , 3
  limit 3 ,3

此处的 3 可以是任意数字

我尝试过这个正则表达式,但我是初学者

"/ limit [0-9]{0,9}+\,[0-9]{0,9} /i$"

我该怎么做

谢谢

最佳答案

$string = 'SELECT
  id,
  name,
  desc,
  (SELECT id as lid FROM comments WHERE lid = id order by id desc limit 1) as lid_com
 FROM posts limit 3 ,3';

echo preg_match('/(limit)\s\d(((\,\s)|(\s\,\s)|(\s\,))\d)?$/i', $string, $matches);  //1
print_r($matches); //$matches['0'] == 'limit 3 ,3'

关于PHP 和正则表达式 : search for word in string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10073351/

相关文章:

php - Preg 替换 html 撇号

php - 将环境变量传递给 php 命令行脚本

mysql - angularjs $http.get 按类别过滤

java - 使用具有不同捕获组的正则表达式将字符串拆分为多个字符串

php - 在 Node.js 中使用 PHP session (存储在 mySql 中)

php - addClause 始终返回空集合

mysql - 如何使用 SQL 连接从两个表中选择表

mysql - Connection.query() 返回未定义的行

javascript - 从JS变量构建正则表达式不起作用

python regex - 使用 re.IGNORECASE 时破折号匹配为数字