I need a Regular Expressions to get the text within 2 tags.
Lets say I want an array returned containing any text within > and
This similar question has answers that will help:
Perl regexp would be
$string =~ /color=rgb\((\d+),(\d+),(\d+)\)/;
@array = ($1,$2,$3);
But you probably need more information that this.
I believe real problems will arise when you want to parse nesting constructs. For example, when you want to examine XML like this
<data><data>123</data><data>456</data></data>
to extract data included in outermost <data>
tags one RegEx alone would not be enough. Just warn you to not use RegEx where some more (powerful and specific) methods exist. Real XML parsers should be considered when doing more complex tasks on XML. My 2 cents...
Since you specifically mentioned C#, here's how I'm doing that exact parsing:
private static readonly Regex RgbValuePattern = new Regex(@"(?<r>\d{1,3}) ?, ?(?<g>\d{1,3}) ?, ?(?<b>\d{1,3})",
RegexOptions.Compiled | RegexOptions.ExplicitCapture);
Then later on...
var match = RgbValuePattern.Match(value);
if (match.Success)
{
int r = Int32.Parse(match.Groups["r"].Value, NumberFormatInfo.InvariantInfo);
int g = Int32.Parse(match.Groups["g"].Value, NumberFormatInfo.InvariantInfo);
int b = Int32.Parse(match.Groups["b"].Value, NumberFormatInfo.InvariantInfo);
return Color.FromArgb(r, g, b);
}
Using Regex to parse XML is usually a really bad idea. See this answer.