Ali Behzadian Nejad
Ali Behzadian Nejad

Reputation: 9044

C# regular expression to validate a string

I have atext file that contains a lot of records like this:

05/11/04+11:10PM+117+04+0218735793+0'00+00:01'51+TR+

or

05/11/04+11:10PM+117+04+0218735793+0'00+00:01'51+TR+

(without INCOMING)

I want to validate these lines and invalidate all other lines(empty lines or comment lines and corrupted lines.

Thanks.

Upvotes: 0

Views: 314

Answers (3)

Rawling
Rawling

Reputation: 50104

I wouldn't try to use a regex for all of it. For example, you have what looks like a date and a time in there, and a couple of other fields that could be times of some kind, which are tricky to do with regular expressions.

I'd handle this with

Upvotes: 3

spender
spender

Reputation: 120380

var regexPattern = @"^\d{2}/\d{2}/\d{2}\+\d{2}:\d{2}(?:AM|PM)\+\d{3}\+\d{2}" +
                   @"\+\d{10}\+\d'\d{2}\+\d{2}:\d{2}'\d{2}\+TR\+$"

Upvotes: 0

Andrew Cheong
Andrew Cheong

Reputation: 30273

^\d\d\/\d\d\/\d\d\+\d\d:\d\d[AP]M\+[\d+':]+\+TR\+$
                                   ^^^^^^^^

I "cheated" in the marked section because I'm not sure exactly what's staying the same, but from the rest of the expression I think you should get the idea.

Upvotes: 0

Related Questions