g.t.w.d
g.t.w.d

Reputation: 621

Regex match for comma delimited string

I have the following string which is legal.

1-5,10-15

Using the following regex, I get a false for match.

^[^-\s]*-?[^-\s]*$

It works fine for things like

which are all legal. But it won't handle comma delimited ranges. What am I missing?

Upvotes: 2

Views: 7451

Answers (8)

Dmitry Ledentsov
Dmitry Ledentsov

Reputation: 3660

where's the handling for a comma? try to visualize your regex in regexper

now try this:

^(\d+-?\d+)(?:\,(\d+-?\d+))+$

screenshot from regexper.com

Update: my regex is not a solution as you might have very specific needs for the captures. However, that nifty tool might help you with the task once you see what your regex does.

Upvotes: 6

Bohemian
Bohemian

Reputation: 424983

Try this regex:

^\d+(-\d+)?(,\d+(-\d+)?)*$

Upvotes: 1

Cameron Tinker
Cameron Tinker

Reputation: 9789

Here's an approach using just splits:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RangeTester
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "1,2,3,1-5,10-15,100-200";
            string[] ranges = str.Split(',');
            foreach (string range in ranges)
            {
                Console.WriteLine(GetRange(range.Trim()));
            }
            Console.Read();
        }

        static string GetRange(string range)
        {
            string[] rng = range.Split('-');
            if (rng.Length == 2)
                return rng[0] + " to " + rng[1];
            else
                return rng[0];
        }
    }
}

I was over thinking the solution to this problem, but since you know that your list of numbers/ranges will be first delimited by commas and then by dashes, you can use splits to parse out the individual parts. There is no need to use regular expressions for parsing this string.

Upvotes: 1

Rahul
Rahul

Reputation: 25

^(\s*\d+\s*-?\s*\d*)(\,(\s*\d+\s*-?\s*\d*))*$

It takes care of starting spaces, followed by at least 1 digit. "-" is optional and can be followed by one or more digits. "," is optional and can be followed by the same group as before.

Matches:

1,5

1-5,5-10

15,2,10,4-10

Upvotes: 1

John Woo
John Woo

Reputation: 263693

Try this pattern,

^\d+(\-\d+)?(\,(\d+(\-\d+)?))*$

it will match on the following strings:

1-5,10-15,5
1,2,3-5,3-4
1-5,10-15
10-15
10-15,5

but not on

1-,10-15,5
1,2,3-5,3-
1-510-15
10-15,
,10-15,5

Upvotes: 2

Sandy
Sandy

Reputation: 11687

I think this should also work fine.

^\d*(-\d*)?,\d*(-?\d*)?$

Hope it helps.

Upvotes: 0

BlackBear
BlackBear

Reputation: 22979

You might want something like (\d+)-(\d+) to get every range. Example here.

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234635

The best regex I know for splitting comma separated strings is:

",(?=(?:[^\""]*\""[^\""]*\"")*(?![^\""]*\""))"

It will not split an entry in quotations that contains commas.

E.g. Hello, There, "You, People" gives

Hello

There

You, People

Upvotes: 1

Related Questions