0

I have a string like " test data \<tag1 attribute1="124" attribut2="abc"\> </tag>test end data"

I need sub string between "<tag1" and ">" as below:

"<tag1 attribute1="124" attribut2="abc">"

I have implemented below regex code for that:

var regex = new Regex("<tag1(.*)>");
var match = regex.Match(result);
var tagContent = match.Groups[1].Value;

Above did not help, it returned empty string.

Can anyone suggest me the right way of extracting content between "<tag1" and ">" using regex?

9
  • 3
    Do not parse XML using Regex Commented Dec 15, 2017 at 10:27
  • 11
    turn back now; if this is html, use a tool like HtmlDocument; if this is xml: use an xml library. This is not a job for regex Commented Dec 15, 2017 at 10:27
  • @SouvikGhosh - I guess he means both, a string in " " in a < >, so bascially he wants to grab the attribute value. Commented Dec 15, 2017 at 10:27
  • @RandRandom Yeah I realized after someone edited the post. Commented Dec 15, 2017 at 10:28
  • Well the end of that tag is \> not >, so did you try <tag1(.*)\\>? Commented Dec 15, 2017 at 10:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.