如何在字符串中使用正则表达式在字符串中找到单词
字符串是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 $”
我该怎么做
谢谢
答案 0 :(得分:1)
"/limit[\s]+[\d]+[\s]*,[\s]*[\d]+$/i"
此搜索案例中包含以下内容:
答案 1 :(得分:1)
$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'
答案 2 :(得分:1)
不记得,LIMIT
是否始终是最后一条SQL语句,所以我使用
/limit\s+(\d+)(?:\s*,\s*(\d+))?(?=[^\n]+\Z)/mi
答案 3 :(得分:0)
试试这个条件 -
SELECT * FROM table WHERE column
REGEXP '(^limit[[:space:]]+[[:digit:]]+[[:space:]]*,[[:space:]]*[[:digit:]]+$)|(^limit[[:space:]]+[[:digit:]]+[[:space:]]*$)';
第一部分找到像'limit 3,3'这样的字符串 -
^limit[[:space:]]+[[:digit:]]+[[:space:]]*,[[:space:]]*[[:digit:]]+$
第二个找到像'limit 3'这样的字符串 -
^limit[[:space:]]+[[:digit:]]+[[:space:]]*$