Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match all the words outside quotation marks?

Is it possible to match text outside quotation marks by using standard regex parser? I have seen this answer, but it is done by using PCRE:

Can regex match all the words outside quotation marks?

This is not a pure solution because of using PERL. I know that it also can be solved by using programming language, but the idea is to use pure regex parser.

I have made something like this, but this is not working correctly

[^'"]*(?=(?:(['"])+(.*?\1))|([^'"]*$))

Thank you in advance.

UPD1:The idea is to match any kind of text outside quotation marks, the solution must not depend on the input.

like image 633
dzharvis Avatar asked Dec 04 '22 05:12

dzharvis


2 Answers

<yourtext>(?=(?:[^"]*"[^"]*")*[^"]*$)

Yes you can do it in using positive lookahead.But this assumes you have balanced " and there is no stray " lying somewhere.See demo.

http://regex101.com/r/sU3fA2/29

like image 103
vks Avatar answered Dec 17 '22 11:12

vks


I came up with this solution:

(?:[^"](?=(?:[^"]*?(?:["][^"]*?["][^"]*?)+$)|(?:[^"]*?$)))*|(^[^"]*["][^"]*$)

http://regex101.com/r/pI8xA4/2

it will not work very well if we have an odd number of quotes - In this case, it will skip the first quote. But it is the best solution for me for now.

like image 39
dzharvis Avatar answered Dec 17 '22 10:12

dzharvis