pego
pego

Reputation: 127

Regex to match all occurances not surrounded by quote and stop at character

It's late here in europe and I can't get that regex running :-( I want to match all text, that is not surrounded by quotes.

INPUT: Oh yeah this;input;'with quotes';can;'be very';tricky;'believe me';boy

It's really easy to match all text surrounded by quotes.

DEMO

But what I want is the opposite. Also the match should be splitted by ; so that I get the following matches:

  1. Oh yeah this
  2. input
  3. can
  4. tricky
  5. boy

Any ideas before I get mad?

thx in advance!

Upvotes: 2

Views: 1640

Answers (2)

shA.t
shA.t

Reputation: 16958

I think you can use a trick like this:

'[^;]*'(?=;|$)|([^;]+)(?=;|$)

Explanation:

'[^;]*'(?=;|$)      => finding words between `'` and ended with `;` or end of text, but not group it
|                   => or
([^;]+)(?=;|$)      => finding other words ended with `;` or end of text, but grouped

now you can use $1 to catch what you want.

[ Regex Demo ]

Upvotes: 2

marvel308
marvel308

Reputation: 10458

you can use the regex

(?<=;|^)[^;'][^;]*[^;'](?=;|$)

see the regex101 demo, regex storm demo

Upvotes: 1

Related Questions