Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think, the Reg-Ex expression to match all Key-Value-Pairse your are whant is:</p> <pre><code>&lt;Context&gt;\s*?&lt;Key&gt;(.*?)\&lt;/Key&gt;\s*?&lt;Value&gt;(.*?)&lt;/Value&gt;\s*?&lt;/Context&gt; </code></pre> <p>Description:</p> <pre><code>// &lt;Context&gt;\s*?&lt;Key&gt;(.*?)\&lt;/Key&gt;\s*?&lt;Value&gt;(.*?)&lt;/Value&gt;\s*?&lt;/Context&gt; // // Match the characters "&lt;Context&gt;" literally «&lt;Context&gt;» // Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?» // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» // Match the characters "&lt;Key&gt;" literally «&lt;Key&gt;» // Match the regular expression below and capture its match into backreference number 1 «(.*?)» // Match any single character that is not a line break character «.*?» // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» // Match the character "&lt;" literally «\&lt;» // Match the characters "/Key&gt;" literally «/Key&gt;» // Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?» // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» // Match the characters "&lt;Value&gt;" literally «&lt;Value&gt;» // Match the regular expression below and capture its match into backreference number 2 «(.*?)» // Match any single character that is not a line break character «.*?» // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» // Match the characters "&lt;/Value&gt;" literally «&lt;/Value&gt;» // Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?» // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» // Match the characters "&lt;/Context&gt;" literally «&lt;/Context&gt;» </code></pre> <p>Usage:</p> <pre><code>using System.Text.RegularExpressions; public static void RunSnippet() { Regex RegexObj = new Regex("&lt;Context&gt;\\s*?&lt;Key&gt;(.*?)\\&lt;/Key&gt;\\s*?&lt;Value&gt;(.*?)&lt;/Value&gt;\\s*?&lt;/Context&gt;", RegexOptions.IgnoreCase | RegexOptions.Multiline); Match MatchResults = RegexObj.Match(@"&lt;ContextDetails&gt; &lt;Context&gt;&lt;Key&gt;ID&lt;/Key&gt;&lt;Value&gt;100&lt;/Value&gt;&lt;/Context&gt; &lt;Context&gt;&lt;Key&gt;Name&lt;/Key&gt; &lt;Value&gt;MyName&lt;/Value&gt;&lt;/Context&gt; &lt;/ContextDetails&gt; "); while (MatchResults.Success){ Console.WriteLine("Key: " + MatchResults.Groups[1].Value) ; Console.WriteLine("Value: " + MatchResults.Groups[2].Value) ; Console.WriteLine("----"); MatchResults = MatchResults.NextMatch(); } } /* Output: Key: ID Value: 100 ---- Key: Name Value: MyName ---- */ </code></pre> <p>The Regular-Expression to math only the Value or the Key "Name":</p> <pre><code>&lt;Context&gt;\s*?&lt;Key&gt;Name&lt;/Key&gt;\s*?&lt;Value&gt;(.*?)&lt;/Value&gt;\s*?&lt;/Context&gt; </code></pre> <p>Description:</p> <pre><code>// &lt;Context&gt;\s*?&lt;Key&gt;Name&lt;/Key&gt;\s*?&lt;Value&gt;(.*?)&lt;/Value&gt;\s*?&lt;/Context&gt; // // Match the characters "&lt;Context&gt;" literally «&lt;Context&gt;» // Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?» // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» // Match the characters "&lt;Key&gt;Name&lt;/Key&gt;" literally «&lt;Key&gt;Name&lt;/Key&gt;» // Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?» // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» // Match the characters "&lt;Value&gt;" literally «&lt;Value&gt;» // Match the regular expression below and capture its match into backreference number 1 «(.*?)» // Match any single character that is not a line break character «.*?» // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» // Match the characters "&lt;/Value&gt;" literally «&lt;/Value&gt;» // Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?» // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» // Match the characters "&lt;/Context&gt;" literally «&lt;/Context&gt;» </code></pre> <p>Usage:</p> <pre><code>string SubjectString = @"&lt;ContextDetails&gt; &lt;Context&gt;&lt;Key&gt;ID&lt;/Key&gt;&lt;Value&gt;100&lt;/Value&gt;&lt;/Context&gt; &lt;Context&gt;&lt;Key&gt;Name&lt;/Key&gt; &lt;Value&gt;MyName&lt;/Value&gt;&lt;/Context&gt; &lt;/ContextDetails&gt; "; Console.WriteLine( Regex.Match(SubjectString, "&lt;Context&gt;\\s*?&lt;Key&gt;Name&lt;/Key&gt;\\s*?&lt;Value&gt;(.*?)&lt;/Value&gt;\\s*?&lt;/Context&gt;", RegexOptions.IgnoreCase | RegexOptions.Multiline).Groups[1].Value ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload