-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?

4
  • 1
    If you are going to parse date, then it's better to use Parse or its overload. Commented Mar 4, 2017 at 15:01
  • why do you say that it's a time but at the end say that the strings start with a date? Commented Mar 4, 2017 at 16:06
  • @LưuVĩnhPhúc I mistakenly wrote this. I edited the post. Commented Mar 4, 2017 at 17:41
  • @revo this is stated in the question. Commented Mar 4, 2017 at 17:42

2 Answers 2

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"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice and clean solution, works perfectly for all the dates and doesn't need manual regex checks. Thanks!
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
}

4 Comments

"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.
I can see you put17.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.
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.
@AliCrash Yes, and it could possibly also be 24.12., but the final dot's always there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.