C# Regex matching two numbers divided by specific string and surrounded by brackets

Solution 1:

The following regex should be fine.

^\[-?\d+(?:\.\d+)? \.\. -?\d+(?:\.\d+)?\)$
var pattern = @"^\[-?\d+(?:\.\d+)? \.\. -?\d+(?:\.\d+)?\)$";
var inputs = new[]{"[10 .. 15)", "[100 .. 15.2)", "[10.431 .. 15)", "[-10.3 .. -5)", "[-10.4 .. 5.12)", "[10.4312 .. -5.1232)", };
foreach (var input in inputs)
{
    Console.WriteLine(input + " = " + Regex.IsMatch(input, pattern));
}

// [10 .. 15) = True
// [100 .. 15.2) = True
// [10.431 .. 15) = True
// [-10.3 .. -5) = True
// [-10.4 .. 5.12) = True
// [10.4312 .. -5.1232) = True

https://dotnetfiddle.net/LpswtI

Solution 2:

You can use

^\[(-?\d{1,4}(?:\.\d{1,4})?) \.\. (-?\d{1,4}(?:\.\d{1,4})?)\)$

See the regex demo. Details:

  • ^ - start of string
  • \[ - a [ char
  • (-?\d{1,4}(?:\.\d{1,4})?) - Group 1: an optional -, one to four digits and then an optional sequence of a . and one to four digits
  • \.\. - a .. string
  • (-?\d{1,4}(?:\.\d{1,4})?) - Group 2: an optional -, one to four digits and then an optional sequence of a . and one to four digits
  • \) - a ) char
  • $ - end of string (use \z if you need to check for the very end of string).

See the C# demo:

var texts = new List<string> { "[10 .. 15)", "[100 .. 15.2)", "[10.431 .. 15)", "[-10.3 .. -5)", "[-10.4 .. 5.12)", "[10.4312 .. -5.1232)", "[12345.1234 .. 0)", "[1.23456 .. 0" };
var pattern = new Regex(@"^\[(-?\d{1,4}(?:\.\d{1,4})?) \.\. (-?\d{1,4}(?:\.\d{1,4})?)\)$");
foreach (var s in texts) 
{
    Console.WriteLine($"---- {s} ----");
    var match = pattern.Match(s);
    if (match.Success) 
    {
        Console.WriteLine($"Group 1: {match.Groups[1].Value}, Group 2: {match.Groups[2].Value}");
    }
    else
    {
        Console.WriteLine($"No match found in '{s}'.");
    }
}

Output:

---- [10 .. 15) ----
Group 1: 10, Group 2: 15
---- [100 .. 15.2) ----
Group 1: 100, Group 2: 15.2
---- [10.431 .. 15) ----
Group 1: 10.431, Group 2: 15
---- [-10.3 .. -5) ----
Group 1: -10.3, Group 2: -5
---- [-10.4 .. 5.12) ----
Group 1: -10.4, Group 2: 5.12
---- [10.4312 .. -5.1232) ----
Group 1: 10.4312, Group 2: -5.1232
---- [12345.1234 .. 0) ----
No match found in '[12345.1234 .. 0)'.
---- [1.23456 .. 0 ----
No match found in '[1.23456 .. 0'.

Solution 3:

This works (see this .Net Fiddle:

using System;
using System.Text.RegularExpressions;
                    
public class Program
{
    public static void Main()
    {
        Match m = rx.Match("[123 .. -9876.5432]");
        if (!m.Success )
        {
            Console.WriteLine("No Match");
        }
        else
        {
          Console.WriteLine(@"left:  {0}", m.Groups[ "left"  ] );
          Console.WriteLine(@"right: {0}", m.Groups[ "right" ] );
        }
    }
    
    private static readonly Regex rx = new Regex(@"
        ^                      # anchor match at start-of-text
        [[]                    # a left square bracket followed by
          (?<left>             # a named capturing group, containing a number, consisting of
            -?[0-9]{1,4}       # - a mandatory integer portion followed by
            ([.][0-9]{1,4})?   # - an optional fractional portion
          )                    # the whole of which is followed by
        [ ][.][.][ ]           # a separator (' .. '), followed by
          (?<right>            # another named capturing group containing a number, consisting of
            -?[0-9]{1,4}       # - a mandatory integer portion followed by
            ([.][0-9]{1,4})?   # - an optional fractional portion
          )                    # the whole of which is followed by
        \]                     # a right square bracket, followed by
        $                      # end-of-text
      ",
      RegexOptions.IgnorePatternWhitespace|RegexOptions.ExplicitCapture
    );
    
}