简体   繁体   中英

Comma-separated time regex c#?

I am trying to write the regular expression for the following pattern

pattern = "Time hh:mm separated by comma + # + Time hh:mm separated by comma";

I am newbie with regular expressions, so after learning Quick start regex tutorial , I wrote this:

(([0-1][0-9]|[2][0-3]):([0-5][0-9]))+(,+(([0-1][0-9]|[2][0-3]):([0-5][0-9]))
?+,*)*+(#)+(([0-1][0-9]|[2][0-3]):([0-5][0-9]))+(,+(([0-1][0-9]|[2][0-3]):([0-5] 
[0-9]))?+,*)*

This regular expression has some problems. It matches some invalid strings also like:

 - 02:00,#03:00

 - 02:00#03:00,

Valid strings:

 - 02:00#03:00

 - 02:00,04:00,06:00#03:00

 - 02:00#03:00,06:00

And it also doesn't create groups properly, I want to create groups in following order:

If string is 02:00,04:00,06:00#03:00 then groups should be:

 - 02:00
 - 04:00
 - 06:00
 - #
 - 03:00

Can anyone please help me to get this regular expression to work as expected?

You can do that without using REGEX like:

string str = "02:00,04:00,06:00#03:00";
TimeSpan temp;
bool ifValid = str.Split(new[] { ',', '#' })
                  .All(r => TimeSpan.TryParseExact(r, @"hh\:mm",CultureInfo.InvariantCulture, out temp));

You can extract it out to a function like:

public bool CheckValid(string str)
{
    TimeSpan temp;
    return str.Split(new[] { ',', '#' })
                      .All(r => TimeSpan.TryParseExact
                                                     (r, 
                                                      @"hh\:mm", 
                                                      CultureInfo.InvariantCulture, 
                                                      out temp));
}

and then check the working like:

List<string> validStrings = new List<string>
{
    "02:00#03:00",
    "02:00,04:00,06:00#03:00",
    "02:00#03:00,06:00"
};
Console.WriteLine("VALID Strings");
Console.WriteLine("============================");
foreach (var item in validStrings)
{
    Console.WriteLine("Result: {0}, string: {1}", CheckValid(item), item);
}

Console.WriteLine("INVALID strings");
Console.WriteLine("============================");
List<string> invalidStrings = new List<string>
{
    "02:00,#03:00",
     "02:00#03:00,",
};

foreach (var item in invalidStrings)
{
    Console.WriteLine("Result: {0}, string: {1}", CheckValid(item), item);
}

Output would be:

VALID Strings
============================
Result: True, string: 02:00#03:00
Result: True, string: 02:00,04:00,06:00#03:00
Result: True, string: 02:00#03:00,06:00
INVALID strings
============================
Result: False, string: 02:00,#03:00
Result: False, string: 02:00#03:00,
\s(([0-1]\d|[2][0-3]):([0-5]\d))((,(([0-1]\d|[2][0-3]):([0-5]\d)))*)#(([0-1]\d|[2][0-3]):([0-5]\d))((,(([0-1]\d|[2][0-3]):([0-5]\d)))*)\s

alternatively:

String time = "(([0-1]\\d|[2][0-3]):([0-5]\\d))";
String timeSequence = time + "((," + time + ")*)";
String regex = "\\s" + timeSequence + "#" + timeSequence + "\\s";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM