-2

I'm really new to Regex, so I'm looking for a way to check if my string starts with a certain regex. I found this on the Internet, however, I can't make it work with a custom regex.

I need to know if a specific line starts with

3.3. XXX

which is how you format German date. So the regex doesn't need to look up only this, but possibly

17.4. XXX

in both cases, I need to know if the input string starts with a date (which can have two possible notations, as stated above). So, for both it'd return true, however, it wouldn't for this:

15G

(for example).

Which regex is good to go for this?

Community
  • 1
  • 1
Çan
  • 105
  • 1
  • 2
  • 10
  • 1
    If you are going to parse date, then it's better to use [Parse](https://msdn.microsoft.com/en-us/library/1k1skd40(v=vs.110).aspx) or its [overload](https://msdn.microsoft.com/en-us/library/kc8s65zs(v=vs.110).aspx). – cassandrad Mar 04 '17 at 15:01
  • why do you say that it's a *time* but at the end say that the strings start with a *date*? – phuclv Mar 04 '17 at 16:06
  • @LưuVĩnhPhúc I mistakenly wrote this. I edited the post. – Çan Mar 04 '17 at 17:41
  • @revo this is stated in the question. – Çan Mar 04 '17 at 17:42

2 Answers2

1

if you want a regex for detecting dd.mm type of date this your answer.

string testregex = "([0-2][0-9]|3[0-1]|[0-9])(.)(0[1-9]|1[0-2]|[0-9])(.)";

you can check any string to find match for this regex, Regex.IsMatch() returns true and statements in if block will execute.

string text="17.4 xxxxxx";
if (Regex.IsMatch(string test,testregex))
{
  //do something
}
Ali Crash
  • 321
  • 1
  • 3
  • 13
  • 1
    "`give me more clear details in comments`". Don't do this, try to keep your Question updated with all relevant details and examples. **Absolutely though, every regex question should have at least a few examples of what should pass, and what should fail, and why**. It is also fine, after you've updated the question, to reply to an answer that the question has been updated. – Regular Jo Mar 04 '17 at 16:30
  • I can see you put`17.4` but not `17.4.`, which is required. As I can see, the dot (.) is missing at the end of the regex. Did you intend to do so? Thanks for your answer anyway. – Çan Mar 04 '17 at 17:45
  • is there always a dot(.) in the end of your target strings? if it is yes, then inform me so I edit the regex in my answer. – Ali Crash Mar 04 '17 at 19:01
  • @AliCrash Yes, and it could possibly also be `24.12.`, but the final dot's always there. – Çan Mar 05 '17 at 10:54
1

Regex is not good at parsing number ranges so it can get pretty messy https://stackoverflow.com/a/15504877/1383168

To check if a string is a valid date you can use DateTime.TryParseExact

string s = "17.4. XXX"; DateTime d;

if (DateTime.TryParseExact(s.Split(' ')[0], "d.M.", 
    System.Globalization.CultureInfo.InvariantCulture, 
    System.Globalization.DateTimeStyles.None, out d)) 
{
    // matches
    Debug.Print($"{d}"); // "4/17/2017 12:00:00 AM"
}
Community
  • 1
  • 1
Slai
  • 19,980
  • 5
  • 38
  • 44
  • Very nice and clean solution, works perfectly for all the dates and doesn't need manual regex checks. Thanks! – Çan Mar 05 '17 at 10:54