As others have already pointed out, you'd probably be on a safer and cleaner side by using an XML parser.
That said, you've already got a working regular expression. Just make sure to retrieve the capture group #1. That will get you just what is inside the first pair of parentheses.
If you're using C#, instead of calling toString()
on the Match
, look into its Groups
property and get its first element:
string pattern = "<from_id>(.*?)</from_id>";
string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?><response list=\"true\"><count>2802</count><post><id>4210</id><from_id>2176594</from_id><to_id>-11423648</to_id><date>1365088358</date><text>dsadsad #ADMIN</text>";
Match match = Regex.Match(input, pattern);
if (match.Success){
System.Console.WriteLine(match.Groups[1].Value);
}
See it working in this Ideone snippet.
If you wanted to get all matches of the pattern, you could use Regex.Matches()
instead, and iterate over each Match
in the MatchCollection
in the same way.