Regular Expression matching double quotes

C

Codex Twin

Hello folks

I am trying to use regular expressions to achieve the following:

If my searchString is: "London Trains" Paris Amsterdam "Berlin Cars"

I want to be able to split this string by those grouped by the double
quotes first, and then the non-quoted words second. So for the above string,
I should get the following matches

"London Trains"
"Berlin Cars"
Paris
Amsterdam

Can this be achieved with a single regex?

My approach has been 3 step:
1. Match by word groups grouped in double quotes
2. Remove the words in the doubled quoted from the string
3. Regex.split the string by ' ' (single white space) to get an string array
of the remaining words (Paris and Amsterdam)

But my attempts have been dashed at 1 and 2.
I am even struggling to find a pattern that matches words in double quotes.

Any pointers would be greatly appreciated. I am using C#.

Thanks
 
W

Wessel Troost

This sample console program matches strings in quotes. It looks for a
",
followed by any number of not-", and then " again.

class Program
{
private static void Test(
string s )
{
Regex r = new Regex("\"(?<InQuote>[^\"]*)\"");
Match m = r.Match( s );
while( m.Success )
{
Console.WriteLine( m.Groups["InQuote"].Value );
m = m.NextMatch();
}
}

static void Main(string[] args)
{
Test( "alfa\"beta\"gamma" );
}
}

Greetings,
Wessel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top