Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match zero or one time a string

Tags:

c#

regex

I'm trying to make a Regex that matches this string {Date HH:MM:ss}, but here's the trick: HH, MM and ss are optional, but it needs to be "HH", not just "H" (the same thing applies to MM and ss). If a single "H" shows up, the string shouldn't be matched.

I know I can use H{2} to match HH, but I can't seem to use that functionality plus the ? to match zero or one time (zero because it's optional, and one time max).

So far I'm doing this (which is obviously not working):

Regex dateRegex = new Regex(@"\{Date H{2}?:M{2}?:s{2}?\}");

Next question. Now that I have the match on the first string, I want to take only the HH:MM:ss part and put it in another string (that will be the format for a TimeStamp object).

I used the same approach, like this:

Regex dateFormatRegex = new Regex(@"(HH)?:?(MM)?:?(ss)?");

But when I try that on "{Date HH:MM}" I don't get any matches. Why?

If I add a space like this Regex dateFormatRegex = new Regex(@" (HH)?:?(MM)?:?(ss)?");, I have the result, but I don't want the space... I thought that the first parenthesis needed to be escaped, but \( won't work in this case. I guess because it's not a parenthesis that is part of the string to match, but a key-character.

like image 933
Louis Kottmann Avatar asked Apr 26 '11 22:04

Louis Kottmann


People also ask

What does RegEx 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

Is used for zero or more occurrences in RegEx?

A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used. A regular expression followed by a plus sign ( + ) matches one or more occurrences of the one-character regular expression.


2 Answers

(H{2})? matches zero or two H characters.

However, in your case, writing it twice would be more readable:

Regex dateRegex = new Regex(@"\{Date (HH)?:(MM)?:(ss)?\}");

Besides that, make sure there are no functions available for whatever you are trying to do. Parsing dates is pretty common and most programming languages have functions in their standard library - I'd almost bet 1k of my reputation that .NET has such functions, too.

like image 194
ThiefMaster Avatar answered Oct 08 '22 02:10

ThiefMaster


In your edit you mention an unwanted leading space in the result… to check a leading or trailing condition together with your regex without including this to the result you can use lookaround feature of regex.

new Regex(@"(?<=Date )(HH)?:?(MM)?:?(ss)?")

(?<=...) is a lookbehind pattern.

Regex test site with this example.

For input Date HH:MM:ss, it will match both regexes (with or without lookbehind).

But input FooBar HH:MM:ss will still match a simple regex, but the lookbehind will fail here. Lookaround doesn't change the content of the result, but it prevents false matches (e.g., this second input that is not a Date).

Find more information on regex and lookaround here.

like image 28
bw_üezi Avatar answered Oct 08 '22 01:10

bw_üezi