Home > Software engineering >  Parse groups of words including if they have quotes regex
Parse groups of words including if they have quotes regex

Time:09-03

foo bar 1 2 "hello world" this is a "line of" text

what regex can I use to capture each group of text, but if anything has quotes around it, capture the whole group:

so it would be

foo, bar, 1, 2, hello world, this, is, a, line of, text

CodePudding user response:

You can use:

(?<=\")\w[\w ]*(?=\")|\w 

DEMO

It first tries to match quoted words then on fail, it use \w to match words.

(?<=\"): lookbehind assertion to check it's after the ".
(?=\"): lookafter assertion to check it's before the ".
\w[\w ]*: This first \w should be there not to allow starting with space. otherwise it catches this is a in a single word.

CodePudding user response:

I suppose this got the job done – someone may have a better solution:

/"(?:[^"\\]|\\.)*"|([\w.:] )/g
  • Related