lxkmxl
lxkmxl

Reputation: 81

Extracting Matches from Regex

Given a string

""Being Ordered Around by You Makes Me Really Angry Somehow!!!" "Whaddaya Mean, 'Somehow'!!?""
""Omae ni Meirei Sareru no wa Nanka Haratatsu!!!" "Nankatte Nani!!?""

How can I extract the string between each set of double quotes (including double quotes in between) so that I get two separate strings like:

"Being Ordered Around by You Makes Me Really Angry Somehow!!!" "Whaddaya Mean, 'Somehow'!!?"

and

"Omae ni Meirei Sareru no wa Nanka Haratatsu!!!" "Nankatte Nani!!?"

The regex I'm currently using is "(.*)(.*)" and it matches okay according to this tool

enter image description here

My problem is extracting the two matches as seperate strings.

var pattern =  new Regex(@"""(.*)(.*)""", RegexOptions.None);
var matches = pattern.Matches(text);

matches doesn't contain two elements. What am I doing wrong?

Upvotes: 0

Views: 104

Answers (2)

SamWhan
SamWhan

Reputation: 8332

Try using Regex.Match instead of Matches, and this regex

"("[^"]*")[^"]*("[^"]*")"

It matches a ", captures a following " and anything up to, and including, the next ". Then it matches anything up to the next ". Then it repeats - captures the following " and anything up to, and including, the next ". Finally it matches the terminating ". The two strings, including their surrounding quotes are in capture group 1 & 2.

Something like this:

    string s1 = "\"\"Being Ordered Around by You Makes Me Really Angry Somehow!!!\" \"Whaddaya Mean, 'Somehow'!!?\"\"",
           s2 = "\"\"Omae ni Meirei Sareru no wa Nanka Haratatsu!!!\" \"Nankatte Nani!!?\"\"";

    Console.WriteLine("Before 1  : " + s1);
    Console.WriteLine("Before 2  : " + s2);

    Regex r = new Regex("\"(\"[^\"]*\")[^\"]*(\"[^\"]*\")\"");
    Match m = r.Match(s1);
    Console.WriteLine("After 1.1 : " + m.Groups[1].Value);
    Console.WriteLine("After 1.2 : " + m.Groups[2].Value);

    m = r.Match(s2);
    Console.WriteLine("After 2.1 : " + m.Groups[1].Value);
    Console.WriteLine("After 2.2 : " + m.Groups[2].Value);

See it here at ideone.

Upvotes: 1

NeverHopeless
NeverHopeless

Reputation: 11233

You may try this regex

("{2}.+?"{2})

and iterate on matches provided by MatchCollection class.

Upvotes: 0

Related Questions