0

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.

  • Is it OK to regular expressions for this purpose?
  • If yes, what is the regular expression?

Thanks.

2
  • Mos def. We don't know if the various fields and delimiters are fixed or variable width, but, yes... this is a good candidate for regex validation. Commented Oct 19, 2012 at 9:47
  • 1
    stackoverflow.com/questions/1991264/regex-query-builder Commented Oct 19, 2012 at 9:49

3 Answers 3

3

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

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

2 Comments

DateTime.ParseExact throws when the parse fails, if this is likely to happen (ie. erroneous records are expected) better to use DateTime.TryParseExact.
Ah, I missed that method. Cheers.
0
^\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.

Comments

0
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\+$"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.