1

I am trying to use Regular Expressions to find a string sequence inside a string.

The pattern i am looking for is:

dd.dd.dddd dd:dd:dd //d is a digit from 0-9

my regex is:

Regex r = new Regex(@"(\d[0-9]{2}.\d[0-9]{2}.\d[0-9]{4}\s\d[0-9]{2}:\d[0-9]{2}:\d[0-9]{2})$");

I am now trying to check, if the string "27.11.2014 09:14:59" is Matching to the regex, but sadly it isn't matching.

string str= "27.11.2014 09:14:59";
Regex r = new Regex(@"(\d[0-9]{2}.\d[0-9]{2}.\d[0-9]{4}\s\d[0-9]{2}:\d[0-9]{2}:\d[0-9]{2})$");
test = r.IsMatch(str,0);

//output: test=false

Anyone knows why the String is not Matching with that regular expression?

2

2 Answers 2

3

\d[0-9]{2} matches three digits:

\d      first digit
[0-9]   second digit
{2}     causes the previous expression ([0-9]) to match again

If you remove all occurences of \d, your pattern should work. You should escape all dots . though, because right now they match any character, not just a ..

Sign up to request clarification or add additional context in comments.

Comments

0

As Rawing already said, the upper Regular expression is trying to match 3 digits instead of one. for everyone who want to know how the regular expression should look like:

@"(\d{2}.\d{2}.\d{4}\s\d{2}:\d{2}:\d{2})$"

Thats working, at least for me.

1 Comment

You should have read his full answer and escaped your . characters.

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.