Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Another option, which is not quite the solution you wanted, but saves you using two separate regexes, is to use named groups, if your implementation supports it.</p> <p>Here is some C#:</p> <pre><code>var regex = new Regex(@"\=(?&lt;Long&gt;[0-9]+)\?|\+(?&lt;Short&gt;[0-9]+)\?"); string test1 = ";1234567890123456?+1234567890123456789012345123=9876543?"; string test2 = ";1234567890123456?+9876543?"; var match = regex.Match(test1); Console.WriteLine("Long: {0}", match.Groups["Long"]); // 9876543 Console.WriteLine("Short: {0}", match.Groups["Short"]); // blank match = regex.Match(test2); Console.WriteLine("Long: {0}", match.Groups["Long"]); // blank Console.WriteLine("Short: {0}", match.Groups["Short"]); // 9876543 </code></pre> <p>Basically just modify your regex to include the names, and then regex.Groups[GroupName] will either have a value or wont. You could even just use the Success property of the group to know which matched (match.Groups["Long"].Success).</p> <p><em>UPDATE:</em> You can get the group name out of the match, with the following code:</p> <pre><code>static void Main(string[] args) { var regex = new Regex(@"\=(?&lt;Long&gt;[0-9]+)\?|\+(?&lt;Short&gt;[0-9]+)\?"); string test1 = ";1234567890123456?+1234567890123456789012345123=9876543?"; string test2 = ";1234567890123456?+9876543?"; ShowGroupMatches(regex, test1); ShowGroupMatches(regex, test2); Console.ReadLine(); } private static void ShowGroupMatches(Regex regex, string testCase) { int i = 0; foreach (Group grp in regex.Match(testCase).Groups) { if (grp.Success &amp;&amp; i != 0) { Console.WriteLine(regex.GroupNameFromNumber(i) + " : " + grp.Value); } i++; } } </code></pre> <p>I'm ignoring the 0th group, because that is always the entire match in .NET</p>
 

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