Solved

Regex: Can't match last space character

Posted on 2011-09-24
3
321 Views
Last Modified: 2012-05-12
String txt has a space at the end.
string rx2 has a space at the end, rx1 does not.
      string txt = "2011-09-24 18:37:54.19 ";
      string rx1 = "^20[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) (2[0-3]|[0-1][0-9]):[0-5][0-9]:[0-5][0-9]";
      string rx2 = "^20[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) (2[0-3]|[0-1][0-9]):[0-5][0-9]:[0-5][0-9] ";      
      Regex r1 = new Regex(rx1);
      Regex r2 = new Regex(rx2);
      Match m1 = r1.Match(txt);
      Match m2 = r2.Match(txt);
      bool x1 = m1.Success;
      bool x2 = m2.Success;

Open in new window

x1 = True
x2 = False

Why can't I match that last space character?
0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36594235
Neither patten takes into account the fractional time (i.e. 19). The reason the first matches is because the pattern matches right up to the period before "19". That is enough to satisfy the pattern. When you added the space in rx2, you essentially stated that the period should be a space, which of course it is not. You need to account for the fractional time. Try these corrections:

string rx1 = "^20[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) (2[0-3]|[0-1][0-9]):[0-5][0-9]:[0-5][0-9]\\.\\d+";
string rx2 = "^20[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) (2[0-3]|[0-1][0-9]):[0-5][0-9]:[0-5][0-9]\\.\\d+ ";

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36594237
The reason the first matches is because the pattern matches right up to the period before "19".
I should make clear that it matches up to, but not including, the period. So basically, up to the 4 in "54".
0
 

Author Closing Comment

by:deleyd
ID: 36594239
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question