10

For example:

string text = 'some text "and some more" and some other "this is a second group" okay the end';.

I'd like to capture all the spaces that are between quotation marks. The end goal is to replace those spaces with commas.

The end goal, for example:

'some text "and,some,more" and some other "this,is,a,second,group" okay the end'

For example, this would do what I'd like in javascript:

text.replace(/(["]).*?\1/gm, function ($0) {
    return $0.replace(/\s/g, ',');
});

Unfortunately, the only tool I have available is the find/replace feature of textmate.

I've found another that does the opposite of what I need, but uses one line like I need:

text.replace(/\s+(?=([^"]*"[^"]*")*[^"]*$)/gm, ',');

Thanks!

Klnh13
  • 103
  • 1
  • 5
  • @shelvacu , So far all my tries have either returned nothing or the opposite of what I need. I'm not sure how to use the lookbehind to get beginning and end quotes and also look for text inside of them. `/(["]).*?\1/gm` found everything in quotes. `/(["]).*?(\s)+?\2.*?\1/gm` and `/(["'])(?:(?=(\s?))\2.)*?\1/gm` just returned the same as above. `/\s+(?=([^"]*"[^"]*")*[^"]*$)/gm` gives me the exact opposite of what I need. – Klnh13 Apr 18 '16 at 22:37
  • 1
    Try `\s+(?=(?:[^"]*"[^"]*")*[^"]*"[^"]*$)` – Wiktor Stribiżew Apr 18 '16 at 22:39
  • 1
    @WiktorStribiżew That did it thank you!! – Klnh13 Apr 18 '16 at 22:42

1 Answers1

12

You can use

\s+(?=(?:(?:[^"]*"){2})*[^"]*"[^"]*$)

See the regex demo

The \s+ matches 1 or more whitespace that is followed by an odd number of double quotes.

Details: The whitespace matching part is simple, the positive lookahead requires

  • (?:(?:[^"]*"){2})* - zero or more sequences of 2 sequences matching 0+ characters other than a " and then a " (0+ "..."s)
  • [^"]*"[^"]* - 0+ characters other than a " followed with a " and again followed with 0+ characters other than a " (an odd quote must be to the right of the currently matched whitespace)
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Mind that this is more a workaround than a solution, this kind of job should be done with some regular programming language, the way described in the question. Oh, [Regex101](https://regex101.com/r/tX1gM4/1) worked again finally. – Wiktor Stribiżew Apr 18 '16 at 22:47