如何使用正则表达式在字符串中找到单词或不在该字符串中找到单词
字符串是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/