Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions: extract all words out of quotes

Tags:

c#

regex

split

By using Regular Expressions how can I extract all text in double quotes, and all words out of quotes in such string:

01AB "SET 001" IN SET "BACK" 09SS 76 "01 IN" SET

First regular expression should extract all text inside double quotes like

SET 001
BACK
01 IN

Second expression shoud extract all other words in string

01AB
IN
SET
09SS
76
SET

For the first case works fine ("(.*?)"). How can I extract all words out of quotes?

like image 665
mbigun Avatar asked Sep 22 '12 10:09

mbigun


People also ask

How to extract text between quotes in Python?

To extract strings in between the quotations we can use findall() method from re library.

What does re search() return?

The re.search() function will search the regular expression pattern and return the first occurrence. Unlike Python re. match(), it will check all lines of the input string. If the pattern is found, the match object will be returned, otherwise “null” is returned.


1 Answers

Try this expression:

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

The groups matched by it will exclude the quotation marks, because they are enclosed in non-capturing parentheses (?: and ). Of course you need to escape the double-quotes for use in C# code.

If the target string starts and/or ends in a quoted value, this expression will match empty groups as well (for the initial and for the trailing quote).

like image 186
Sergey Kalinichenko Avatar answered Nov 05 '22 13:11

Sergey Kalinichenko