Chandra Eskay
Chandra Eskay

Reputation: 2203

get text inside quotes using regex

I want to extract the data inside the quotes using regex

My Text is : boundary="s323sd2342423---"

Now i need to extract the value inside the double quotes without using substring.

I tried the following but didnt help.

String pattern = @"boundary=""(?<value>[^""]*";
Match m = Regex.Match(rawMessage, pattern);
while (m.Success)
{
    boundaryString = m.Groups["value"].Value;
    m = m.NextMatch();
}

Upvotes: 1

Views: 1512

Answers (4)

NeverHopeless
NeverHopeless

Reputation: 11233

As per you details:

I want to extract the data inside the quotes using regex

Why not you just use this pattern:

"(?<value>[^"]+)"

Upvotes: 0

Pierluc SS
Pierluc SS

Reputation: 3176

With the following Regex you'll get what you want without any grouping

(?<=boundary=")[^"]+(?=")

Code to get the the quoted text:

string txt = "boundary=\"s323sd2342423---\"";
string quotedTxt = Regex.Match(txt, @"(?<=boundary="")[^""]+(?="")").Value;

Upvotes: 1

I4V
I4V

Reputation: 35353

You can use this pattern and it will work.

String pattern = @"boundary=\""(?<value>.+?)\""";

Upvotes: 1

Ilya Ivanov
Ilya Ivanov

Reputation: 23626

You need to close opening bracket of a group

String pattern = @"boundary=""(?<value>[^""]*)";

now if you go with

Console.WriteLine(m.Groups["value"].Value);

will print:

s323sd2342423---

Upvotes: 2

Related Questions