startNet
startNet

Reputation: 274

Preparing regex for text

I have a piece of text:

"text 1

Sentence 1."*"the (1)
 /ðiː/

Sentence 1 Translated."
"text 2

Sentence 2."*"of (2)
 /əv/

Sentence 2 Translated."
"Text 3

Sentence 3!"*"and (3)
 /ænd/

Sentence 3 Translated!"

How to get values in such a division from " to the nearest " Get them all as a collection

"text 1

Sentence 1."*"the (1)
 /ðiː/

Sentence 1 Translated."

I trying this, but not work:

"(.*?)""

Upvotes: 1

Views: 77

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626803

Judging by your example string, you need to get substrings from a " that is the first char on a line up to the first " that ends a line.

If you have the text inside a single variable, say text, you may use

var results = Regex.Matches(text, @"(?sm)^("".*?"")\r?$")
        .Cast<Match>()
        .Select(m => m.Groups[1].Value);

See the regex demo

Pattern details

  • (?sm) - RegexOptions.Singleline and RegexOptions.Multiline are on
  • ^ - start of a line
  • ("".*?"") - Group 1: ", any 0+ chars but as few as possible, and a "
  • \r? - an optional CR (as $ does not match before a CR)
  • $ - end of line.

Upvotes: 1

Related Questions