Select Specific Portion of Regex Search

时间:2016-02-12 20:01:09

标签: regex search sublimetext2

In Sublime Text 2 I want to select just the 4th occurrence of a double-quote in a line that contains at least 7 double-quotes. I want to do this with a find all so each of the 4th occurrences of the double-quotes is selected. I have the following search criteria to find lines with at least 7 double-quotes:

".*".*".*".*".*".*".*

I want a cursor at each 4th quote like this (| meaning cursor):

"test",123,456,"more |"testing", "test1"
"test",123,456,"other |"testing", "test2"

"Find All" selects the full row and not just the 4th double-quote. I know you can mostly the same things using a replace instead of cursors in each location. For example:

Find What: (".*".*".*".*)"(.*".*".*)
Replace With: $1<anything you want>$2

But I really want the option to type with cursors at the point of each 4th quote as there is additional functionality I'd like to use.

1 个答案:

答案 0 :(得分:1)

You can use the following regex to match the 4th quote:

^(?:[^"\n]*"){3}[^"\n]*\K"(?=(?:[^"\n]*"){3})

enter image description here

or the position before the 4th double quote:

^(?:[^"\n]*"){3}[^"\n]*\K(?="(?:[^"\n]*"){3})

enter image description here

See demo

The regex matches:

  • ^ - start of line
  • (?:[^"\n]*"){3} - 3 occurrences of zero or more characters other than double quote and a newline followed with a double quote, followed with...
  • [^"\n]* - zero or more characters other than double quote and a newline
  • \K - an operator omitting all the text matched so far
  • " - the 4th double quote on a line
  • (?=(?:[^"\n]*"){3}) - that is followed with 3 occurrences of zero or more characters other than double quote and a newline followed with a ".