Extracting a portion of a string

  • Thread starter Thread starter Richard L Rosenheim
  • Start date Start date
R

Richard L Rosenheim

I have some text where I need to extract some pieces from. The text will be
in a format like this:

a string description color="red" type="unknown"

In the above example, I would be looking to extract the word "red".

There's couple of ways I could approach the problem. I could use IndexOf to
search for the string 'color=' and then extract the value using the Substr
method. Or, I could use a regular expression like:
color="([a-z]|[A-Z])+"

In this case, the Match method would return the string 'color="red"'. I
could then use the Substr method to extract the string 'red'. Of course,
using a regular expression might be a little overkill in this case.

I'm just wondering if there is, buried in the 1500+ classes of .NET, a
better approach. Or, if there is a way of getting a regular expression to
return just the string within the quotes.

Richard
 
The following C# code should do the trick (using a regular expression):

Regex rgx = new Regex(@"color=\""(?<color>\S*)\""");
Match m = rgx.Match("a string description color=\"red\" type=\"unknown\"");
if (m.Success)
{
string color = m.Groups["color"].Value;
Console.WriteLine(color);
}

HTH, Jakob.
 
Richard,

As most things with dotNet there is not a "best" method.

I always write when someone ask that as you that there than would be only
one method.

Mostly is it a matter of preference or better said the knowledge of a
programmer from the classes.

I will avoid forever a regex (because I don't like any kind of cryptic
code), where others take it direct because they use it without it seems
thinking ("thinking" not related too the first part of the sentence).

I would probably go in your situation for an IndexOf and the Substring (not
the Substr that is Scripting)

Just my thought,

Cor
 
Thanks. It works -- just what I was looking for.

Richard


Jakob Christensen said:
The following C# code should do the trick (using a regular expression):

Regex rgx = new Regex(@"color=\""(?<color>\S*)\""");
Match m = rgx.Match("a string description color=\"red\" type=\"unknown\"");
if (m.Success)
{
string color = m.Groups["color"].Value;
Console.WriteLine(color);
}

HTH, Jakob.

Richard L Rosenheim said:
I have some text where I need to extract some pieces from. The text will be
in a format like this:

a string description color="red" type="unknown"

In the above example, I would be looking to extract the word "red".

There's couple of ways I could approach the problem. I could use IndexOf to
search for the string 'color=' and then extract the value using the Substr
method. Or, I could use a regular expression like:
color="([a-z]|[A-Z])+"

In this case, the Match method would return the string 'color="red"'. I
could then use the Substr method to extract the string 'red'. Of course,
using a regular expression might be a little overkill in this case.

I'm just wondering if there is, buried in the 1500+ classes of .NET, a
better approach. Or, if there is a way of getting a regular expression to
return just the string within the quotes.

Richard
 
Back
Top