Find one quote at the end of the line starting with "mySqlQueryToArray"

I am trying to use a regex to find single quotes (so I can turn them all into double quotes) anywhere in a string that starts with mySqlQueryToArray (a function that makes a query to a SQL DB). I'm doing regex in Sublime Text 3, which I'm pretty sure uses Perl Regex. I would like to have a regex match with every single quote in a line, so that, for example, I can have a line:

mySqlQueryToArray($con, "SELECT * FROM Template WHERE Name='$name'");

      

I want the regex to match in this string with quotes around it $name

, but there were no other characters in that string. I am trying to use (?<=mySqlQueryToArray.*)'

but it tells me the appearance of the statement is invalid. I have also tried (?<=mySqlQueryToArray)(?<=.*)'

but this is not valid either. Can anyone guide me to a regex that will do what I need?

+3


source to share


4 answers


fooobar.com/questions/2157206 / ... is the best answer.

I'm not good enough with RegEx or ST to do this in one step. But I can do it in two:

1 / Search all strings mySqlQueryToArray


Open the search bar: ⌘For Find->Find...


Make sure you select the Regex ( .*

) button (bottom left) and the wrapper selector (all others should be off) Search:^\s*mySqlQueryToArray.*$

  • ^

    start of line
  • \s*

    any indent
  • mySqlQueryToArray

    your call
  • .*

    all that's worth
  • $

    end of line

Click Find All


This will select each event that you want to change.

2 / Enter overwrite mode
βŒ₯⌘For Find->Replace...


This time make sure wrap, Regex AND In select are active 3E7kD.png.
Find them '([^']*)'

and replace them with "\1"

.



  • '

    - your single quotes
  • (...)

    si capture block referenced \1

    in the replacement field
  • [^']*

    for any character that is not a single quote is repeated

Then press Replace All

I know this is a little more complicated than the other answer, but this case covers cases where your string will contain multiple strings with a single quote. Like this:

mySqlQueryToArray($con, "SELECT * FROM Template WHERE Name='$name' and Value='1234'");

      

If that's too much, I think something like find: (?<=mySqlQueryToArray)(.*?)'([^']*)'(.*?)

and replacing it with \1"\2"\3

will suffice.

+1


source


To find any number of single quotes in a string starting with your keyword, you can use the anchor \G

("end of last match") by replacing:

(^\h*mySqlQueryToArray|(?!^)\G)([^\n\r']*)'

      

C \1\2<replacement>

: see demo here .

Explanation



( ^\h*mySqlQueryToArray  # beginning of line: check the keyword is here
 | (?!^)\G )             # if not at the BOL, check we did match sth on this line
( [^\n\r']* ) '          # capture everything until the next single quote

      

The general idea is to match everything up to the next single quote with ([^\n\r']*)'

in order to replace it with \2<replacement>

, but only if that's all:

  • immediately after the start keyword ( ^mySqlQueryToArray

    ) or
  • after the end of the last match ( (?!^)\G

    ): in this case, we know that we have the keyword and are on the corresponding line.

\h*

allows for any leading padding, as Xælias suggested ( \h

being a shortcut for any horizontal space).

+2


source


You can use regex like this:

(mySqlQueryToArray.*?)'(.*?)'(.*)

      

Working demo

Check the replacement section.

0


source


You can use \K

see this regex:

mySqlQueryToArray[^']*\K'(.*?)'

      

Here is the regex demo .

0


source







All Articles