Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a regular expressions solution matching your requirements. Bear in mind that this will probably not work if you decide to alter your requirements in the slightest bit, which is faithful to the <a href="https://stackoverflow.com/questions/58640/great-programming-quotes/58646#58646">well known quote here</a>.</p> <pre><code>using System.Text.RegularExpressions; string[] samples = { @"Hellotoevryone&lt;img height=""115"" width=""150"" alt="""" src=""/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg"" /&gt;Iamsogladtoseeall.", "Testing123Hello.World", @"Test&lt;a href=""http://stackoverflow.com""&gt;StackOverflow&lt;/a&gt;", @"Blah&lt;a href=""http://stackoverflow.com""&gt;StackOverflow&lt;/a&gt;Blah&lt;a href=""http://serverfault.com""&gt;ServerFault&lt;/a&gt;", @"Test&lt;a href=""http://serverfault.com""&gt;Server Fault&lt;/a&gt;", // has a space, not matched "Stack Overflow" // has a space, not matched }; // use these 2 lines if you don't want to use regex comments //string pattern = @"^((?:\S(?:\&lt;[^&gt;]+\&gt;)?){1,10})+$"; //Regex rx = new Regex(pattern); // regex comments spanning multiple lines requires use of RegexOptions.IgnorePatternWhitespace string pattern = @"^( # match line/string start, begin group (?:\S # match (but don't capture) non-whitespace chars (?:\&lt;[^&gt;]+\&gt;)? # optionally match (doesn't capture) an html &lt;...&gt; tag # to match img tags only change to (?:\&lt;img[^&gt;]+\&gt;)? ){1,10} # match upto 10 chars (tags don't count per your example) )+$ # match at least once, and match end of line/string "; Regex rx = new Regex(pattern, RegexOptions.IgnorePatternWhitespace); foreach (string sample in samples) { if (rx.IsMatch(sample)) { foreach (Match m in rx.Matches(sample)) { // using group index 1, group 0 is the entire match which I'm not interested in foreach (Capture c in m.Groups[1].Captures) { Console.WriteLine("Capture: {0} -- ({1})", c.Value, c.Value.Length); } } } else { Console.WriteLine("Not a match: {0}", sample); } Console.WriteLine(); } </code></pre> <p>Using the samples above, here's the output (numbers in parentheses = string length):</p> <pre><code>Capture: Hellotoevr -- (10) Capture: yone&lt;img height="115" width="150" alt="" src="/Content/Edt/image/b49768 75-8dfb-444c-8b32-cc b47b2d81e0.jpg" /&gt;Iamsog -- (116) Capture: ladtoseeal -- (10) Capture: l. -- (2) Capture: Testing123 -- (10) Capture: Hello.Worl -- (10) Capture: d -- (1) Capture: Test&lt;a href="http://stackoverflow.com"&gt;StackO -- (45) Capture: verflow&lt;/a&gt; -- (11) Capture: Blah&lt;a href="http://stackoverflow.com"&gt;StackO -- (45) Capture: verflow&lt;/a&gt;Bla -- (14) Capture: h&lt;a href="http://serverfault.com"&gt;ServerFau -- (43) Capture: lt&lt;/a&gt; -- (6) Not a match: Test&lt;a href="http://serverfault.com"&gt;Server Fault&lt;/a&gt; Not a match: Stack Overflow </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