tags:

views:

42

answers:

1

I have a script file containing 1000 lines of data and some of the lines contain some date string in the form of dd/mm/yyyy hh:mm:ss format. My objective is to find out the lines which contain a date of the following pattern

"Value=10/08/2010 13:39:37", ENDITEM,

Some example lines which contains dates in the script are

"Name=s_1_1_81_0", "Value=10/08/2010 13:39:37", ENDITEM,
// {Siebel_Parse_Web_Page72_S_BC2_S40_R02_F30} = "07/27/2010"   (Some Date)
Number0*12*Install Date19*08/24/2010 00:00:0015*Unit of Measure9*Per Month19*To Service

I want to find out ONLY those lines similar to the first example not the other ones, i.e. the lines containing date string which starts with "Value= and ends with ",

Can you guys please help me out here?

PS: I want the code in C# please.

+4  A: 

It looks like you want lines that contains a match of the following pattern:

"Value=\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}", ENDITEM,

As a @-quoted C# string literal, it's:

@"""Value=\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}"", ENDITEM,"

The \d is the character class shorthand for the digit character class. The {n} is finite repetition specifier. Thus, \d{4} matches exactly 4 digits.

You can make the pattern less or more specific, e.g. year 9999 not being matched, etc, but it's probably not worth the effort performing numeric range check in regex.

See also

Related questions

polygenelubricants
...or you simply double up the \ to \\d!
cordellcp3
Thanks for the answer it worked. But a little more help. I have the regex pattern which correctly identify the date is (([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2})$|^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2}\s([0-1]\d|[2][0-3])\:[0-5]\d\:[0-5]\d)But the problem is if I want to use this pattern with your solution (putting the regex pattern in between "Value= and ", ENDITEM,) it is not working. Can you please give me the tailored pattern? Thanks again.
Anindya Chatterjee
@Anindya: Where did you get that pattern? Because that's matching entirely different strings (e.g. year must be in 21st century, among other non-obvious things). And are you sure you need to be this specific with the digits, because that's precisely what I warned about in my current answer. You can read the linked page on numeric ranges and try to fix the problem yourself too, if you want to learn some regex. The related question also answers similar question. There is a lot of learning resources in the two links I provided in this answer.
polygenelubricants
Ok, its fine enough if I use your pattern. I'll use that. Thanks.
Anindya Chatterjee
@Anindya: By the way, the problem with just inserting that pattern into your larger pattern is that your date pattern is anchored with `^` and `$`. You'd probably want to get rid of those if you want this pattern to match unanchored. http://www.regular-expressions.info/anchors.html Also note that despite being quite long, your pattern is still not "specific" enough, e.g. it will match `00/00/2000`, which is not a valid date. You can craft a very specific regex for this, but it's going to be even more complex than the one you have. Again, all the learning resources are linked in my answer.
polygenelubricants