英文:
How to find query with regular expression that have single quote within / enclosed with single quotes
问题
我有问题,我找不到包含单引号的句子,或者在我的情况下,包含在单引号内的MySQL查询字符串。
例如:
UPDATE `blog` SET `BlogDesc` = 'dummy texts' , `BlogHeadlinePic` = 'images12.png' where `BlogId` = 'abcdef';
update `blog` set `BlogDesc` = 'lorem's ipsum' , `BlogHeadlinePic` = 'images.png' where `BlogId` = 'abc'; #需要检测此查询
那么正确的正则表达式是什么,以捕获此模式?
英文:
as the title, i having problem on finding sentences, or in my case a MySQL query string that have single quotes within an enclosed single quotes words.
For example :
UPDATE `blog` SET `BlogDesc` = 'dummy texts' , `BlogHeadlinePic` = 'images12.png' where `BlogId` = 'abcdef';
update `blog` set `BlogDesc` = 'lorem's ipsum' , `BlogHeadlinePic` = 'images.png' where `BlogId` = 'abc'; #need to detect this query
so what is the right reguler expression to catch this pattern occured
答案1
得分: 1
请尝试以下内容:
^.*'(?:[^'=`;]*'){2}.*;$
-
^
表示行/字符串的开头。 -
.*
匹配零个或多个字符,注意这会匹配在'lorem's ipsum'
之前的整个字符串。 -
'
匹配一个单引号,我将其称为引号1,例如,在'lorem's ipsum'
中的第一个单引号,位于单词lorem
之前。 -
(?:[^'=`;]*'){2}
非捕获组。{2}
表示整个组匹配两次。第一次:
-
[^'=`;]*
匹配零个或多个字符,除了'
、=
、`
和;
,例如,这将匹配'lorem's ipsum'
中的单词lorem
。 -
'
匹配一个单引号。我将其称为引号2,例如,在'lorem's ipsum'
中的第二个单引号,位于单词lorem
之后。
第二次
[^'=`;]*
匹配零个或多个字符,除了'
、=
、`
和;
,例如,这将匹配'lorem's ipsum'
中的字符串s ipsum
。'
匹配一个单引号。我将其称为引号3,例如,在'lorem's ipsum'
中的第三个单引号,位于单词ipsum
之后。
-
-
.*
匹配零个或多个字符,注意这会匹配在'lorem's ipsum'
之后的整个字符串。 -
;
确保字符串(在您的情况下是查询)以;
结尾。 -
$
确保它是行/字符串的结尾。
请查看正则表达式演示
编辑
如果您只想获取类似 'lorem's ipsum'
的内容而不是整个查询,请使用以下正则表达式。
'(?:[^'=`;]*'){2}
请查看正则表达式演示
英文:
Try this:
^.*'(?:[^'=`;]*'){2}.*;$
-
^
the start of a line/string. -
.*
match zero or more character, note this will match all the string before'lorem's ipsum'
. -
'
match a single quote, I will call it quote1, e.g., this is the first single quote in'lorem's ipsum'
, which is before the wordlorem
. -
(?:[^'=`;]*'){2}
non-capturing group.{2}
means match the whole group two times.First time:
-
[^'=`;]*
match zero or more characters except'
,=
,`
and;
, e.g., this will match the wordlorem
in'lorem's ipsum'
-
'
match a single quote. I will call it quote2, e.g., this is the second single quote in'lorem's ipsum'
, which is after the wordlorem
.
Second time
[^'=`;]*
match zero or more characters except'
,=
,`
and;
, e.g., this will match the strings ipsum
in'lorem's ipsum'
.'
match a single quote. I will call it quote3, e.g., this is the third single quote in'lorem's ipsum'
, which is after the wordipsum
.
-
-
.*
match zero or more character, note this will match all the string after'lorem's ipsum'
-
;
ensures that the string in your case the query ends with;
. -
$
ensure it is the end of the line/string.
See regex demo
Edit
If you only want to get things like 'lorem's ipsum'
and not the whole query then just use this regex.
'(?:[^'=`;]*'){2}
See regex demo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论