A
A
Anton2022-02-02 15:04:10
Regular Expressions
Anton, 2022-02-02 15:04:10

How to find all commas except those in quotes?

Good afternoon.

There is this text: 1,',',2,'text','word, word2'
How can I find all the commas in this text, except those in single quotes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2022-02-02
@dollar

Varies, depending on the environment/language where the regular expressions are executed.
Usually in several stages, and not even always reg. expressions are needed/suitable.
When using the same reg. expressions, it is desirable to rely on some specific conditions (for example, no more than one comma inside quotes). In general terms, this will be a parser, not a regular expression. But even the parser will have to decide on the conditions, otherwise, in general, it will need to be able to parse this:
,1, ','_,2,'text',x'word, ''\'\\word2'

find all commas in this text, except those in single quotes

In this form, it is solved simply, if we agree that there is no excess of quotes, escaped quotes and other game:
(?:[^,']*'[^']*'[^,']*|[^,']*)(,)(?:[^,']*'[^']*'[^,']*|[^,']*)

https://regex101.com/r/h1A6ZP/1
All outer commas will be in the first group (green on regex101).
But if game is possible, then you will have to invent a combine harvester or abandon regular seasons.

A
Alexander Karabanov, 2022-02-02
@karabanov

What do you mean https://regex101.com/r/SEN5g0/1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question