0

I have a xml file:

"<?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>...

I want to take a string between <from_id> and </from_id>.

I have a regex exprassion <from_id>(.*?)</from_id>, but it return string with tags. Can you help me? Waiting for response) P.S.: Sorry for my poor english!

IanGraham
  • 45
  • 1
  • 1
  • 3
  • 1
    You have to specifically choose to select captured group 1. Are you using a specific language or app? –  Apr 04 '13 at 15:57
  • I using c# somethink like this - string who = Regex.Match(str, "(.*?)").ToString(); – IanGraham Apr 04 '13 at 15:59
  • 3
    Why not use a XML parser to read (safer/cleaner) from the XML document? You would have to check the documentation to see how to capture certain groups or just output the returned (array?) data and see if another index might contain the captured group. I re-tagged the question for you. –  Apr 04 '13 at 16:02
  • 1
    @Joce not a duplicate per se. It seems to be absent of knowledge of the possibilities of solving this case. –  Apr 04 '13 at 16:04

1 Answers1

10

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.

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • Not sure, as I never work with .NET, but read something about it now and then. Isn't .NET only possible with C/C++, or also with C#? –  Apr 04 '13 at 16:05
  • For what reason you are using List? – IanGraham Apr 04 '13 at 16:06
  • @user2037334 Nevermind, there's no reason for the list. You could use a list, however, if you wanted to retrieve a list of matches in the XML instead of a single one. Then you could iterate the matches and get capture group #1 of each one to get the desired results. – Xavi López Apr 04 '13 at 16:18