regex - Verifying the format of a text file? -
i have application downloads .txt
file via ftp, , calls sql insert file data database. want check whether file in correct format before trying insert.
the file should consist of number of rows, each following format:
(4 letter code) (tab) (3 or 4 letter code) (tab) (date dd-mmm-yy) (tab) (variable length text description) (tab) (1 letter code)
is there way check whether every line in file follows pattern, , warn me if doesn't?
sounds typical use case regular expressions. create regex pattern , match against each line in file.
if aren't familiar regex, maybe you'll find this link helpfull in addition links above.
the pattern looking propably this:
^\w{4}\t\w{3,4}\t\d{2}\-\d{3}\-\d{2}\t.*\t\w$
it match text these (imagine _ tab):
test_foo_12-feb-11_this text_x test_fbar_01-jan-15_bla bka bla_o
but not:
test123_foo_12-feb-11_this text
you this:
dim pattern = "^\w{4}\t\w{3,4}\t\d{2}\-\d{3}\-\d{2}\t.*\t\w$" each line in text console.writeline(string.format("{0} {1}", _ line, _ if(regex.ismatch(line, pattern), "is valid", "is invalid")) next
Comments
Post a Comment