Depa
Depa

Reputation: 509

Regular Expression doesn't Match with string

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?

Upvotes: 1

Views: 627

Answers (2)

Depa
Depa

Reputation: 509

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.

Upvotes: 0

Aran-Fey
Aran-Fey

Reputation: 43306

\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 ..

Upvotes: 3

Related Questions