3

I would like to find the third occurrence of string inside quotes in order to replace this string or part of this string. This is a typical line I have to deal with:

"/activities/absenceactivity/lang/#LANG_ID#/prop_dialog.php";"BA2_PD_SCR";"Opis dogodka";"Event description";"Descrição do Evento";"ÐÐ¿Ð¸Ñ Ð¿Ð¾Ð´ÑÑ";"";"č®vykio aprašymas";"Descripción del evento";"";

I know that "([^"]*)" shows every occurrence of text and quotes but I would like to get just the third one, in this example "Opis dogodka" in order to perform Search & Replace in Sublime Text.

Problem is to find the third occurrence of string within the quotes, replace it entirely or just partially and make sure that the Regex provides also a solution for an empty

""

strings.

Thank you.

Rob
  • 14,746
  • 28
  • 47
  • 65
andrej
  • 321
  • 1
  • 4
  • 13
  • Possible duplicate of [RegEx: Match nth occurence](https://stackoverflow.com/questions/28438693/regex-match-nth-occurence) – ashwin153 Oct 10 '17 at 16:31

2 Answers2

1

I'm sure there are ways to simplify this further, but if you're ok with brute force:

Sublime command:

 Find: "[^"]*";"[^"]*";"([^"]*)".*
 Replace: $1

NP++:

 Find what: "([^"]*)";"([^"]*)";"([^"]*)".*
 Replace with: $3

sed:

 sed 's/"\([^"]*\)";"\([^"]*\)";"\([^"]*\)".*/\3/'
  • Sublime command: "[^"]*";"[^"]*";"([^"]*)".* finds the whole line but I don't know what $1 does. I need to replace particular string within the third quote position and sometimes this is "the whole string within quotes" and in some instances just a substring, for instance just character "s". – andrej Oct 10 '17 at 19:51
  • Notice the parenthesis in the search string. `$1` represents the data in the first set of parentheses from the search string. Check out http://www.grymoire.com/Unix/Regular.html#uh-10 – CoreyJJohnson Oct 11 '17 at 01:19
  • @andrej It may help it you write out an example of the desired replacement outcome. – CoreyJJohnson Oct 11 '17 at 01:34
0

You can use {} pattern repetition:

/(?:"([^"]*)";){3}/   # first match group will be 'Opis dogodka'

Demo

Or, use a global type match and then take the third match. This might require logic such as slicing or looping:

/"([^"]*)";/g

Demo 2

Or, just manually put in the first two patterns to skip:

 /^"[^"]*";"[^"]*";("[^"]*";)/

Demo 3

Or combine repetition to skip the first n-1 then capture the nth match:

/^(?:"[^"]*";){2}("[^"]*";)/

Demo 4

dawg
  • 98,345
  • 23
  • 131
  • 206