Spen D
Spen D

Reputation: 4345

Regex Contains in the XML element

How can I use "contains" in the regex ("Contains" or "%like%")?

I have a regex to match the XML node with exact text:

<([\w]+)[^>]*>sample<\/\1>

It yields the exact Node name, but I want to apply the regex like in C# and SQL (%LIKE%).

Text:

    <Part>this is sample part</Part>
    <Remarks>this is sample remark</Remarks>
    <Notes>this is sample notes</Notes>
    <Desc>sample</Desc>

Expected regex result should return all the above nodes, but currently it returns only the last node.

I created a sample here to test.

Upvotes: 2

Views: 3609

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627469

You may use XDocument to parse XML like this:

var s = @"<?xml version=""1.0""?>
  <root>
    <Part>this is sample part</Part>
    <Remarks>this is sample remark</Remarks>
    <Notes>this is sample notes</Notes>
    <Desc>sample</Desc>
  </root>";
var document = XDocument.Parse(s);
var names = document.Descendants()
               .Elements()
               .Where(x => x.Value.Contains("sample")) // all nodes with text having sample
               .Select(a => a.Name.LocalName); // return the local names of the nodes
Console.WriteLine(string.Join("\n", names));

It prints:

enter image description here

The same can be achieved with an XPath:

var names2 = document.Root.XPathSelectElements("//*[contains(text(), \"sample\")]");
var results = names2.Select(x => x.Name.LocalName));

To fall back to regex in case the XML is not valid, use

<(?:\w+:)?(\w+)[^<]*>[^<]*?sample[^<]*</(?:\w+:)?\1>

See the regex demo. Note the (?:\w+:)? matches arbitrary namespace in the open and close tag nodes. [^<] matches any char but <, so it won't overflow to the next node.

Upvotes: 2

Anton Sorokin
Anton Sorokin

Reputation: 401

You are looking for exact match of the "sample" string inside any tag not the string containing "sample" as substring. You can fix your expression as following to get all the lines:

<([\w]+)[^>]*>[a-zA-Z ]*sample[a-zA-Z ]*<\/\1>

Upvotes: 1

Related Questions