如何使用正则表达式查找带有单引号并在单引号内用斜杠括起来的查询。

huangapple go评论63阅读模式
英文:

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 word lorem.

  • (?:[^'=`;]*'){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 word lorem 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 word lorem.

    Second time

    • [^'=`;]* match zero or more characters except ', =,` and ;, e.g., this will match the string s 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 word ipsum.
  • .* 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

huangapple
  • 本文由 发表于 2023年1月9日 11:49:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定