Get values inside quotes from a string

693 Views Asked by At

I have a string something like this:

<BU Name="xyz" SerialNo="3838383" impression="jdhfl87lkjh8937ljk" />

I want to extract values like this:

Name = xyz SerialNo = 3838383 impression = jdhfl87lkjh8937ljk

How to get these values in C#?

I am using C# 3.5.

1

There are 1 best solutions below

0
On BEST ANSWER

If by some reason you don't want to use Xml parser you can use reqular expression to achieve this.

Use this regular expression:

(\w)+=\"(\w)+\"

Use this regular expression like this:

   var input = @"<BU Name=""xyz"" SerialNo=""3838383"" impression=""jdhfl87lkjh8937ljk"" />";
   var pattern = @"(\w)+=\""(\w)+\""";
   var result = Regex.Matches(input, pattern);
   foreach (var match in result.Cast<Match>())
   {
       Console.WriteLine(match.Value);
   }

Result:

//Name="xyz"
//SerialNo="3838383"
//impression="jdhfl87lkjh8937ljk"
//Press any key to continue.