Home > front end >  How to find query with regular expression that have single quote within / enclosed with single quote
How to find query with regular expression that have single quote within / enclosed with single quote

Time:01-09

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

CodePudding user response:

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

  • Related