Read from a text file merging data from subsequent rows



I have to read from a text file. The text in the file is as follows:

The problem I have on this text is that some rows are wrapped, like in the Account line. When I assigning the to Account I only get the value of the Account row, but not the next row that bellows to it.

What I get assigned for Account is:
Account: ‘126201’,‘272001’, ‘272002’, ‘272004’, ‘272005’, ‘272006’, ‘272007’, ‘272008’, ‘272009’, ‘272010’,

Instead of

Account: ‘126201’,‘272001’, ‘272002’, ‘272004’, ‘272005’, ‘272006’, ‘272007’, ‘272008’, ‘272009’, ‘272010’, ‘273001’, ‘273002’, ‘273004’, ‘273005’, ‘273006’, ‘273007’, '273009’

I’m reading the text file with a sequence like:

And I’m reading the contents of the Account like:

What can I do to add the next row into Account (or any of the other variables) if the lines have been wrapped?

Any ideas?



Are you doing split text by Environment.New line? If so, i don’t think you have to worry about wrapped text.

arrLines = Split(txtSample,Environment.NewLine)



I would solve this kind of problem using Regular Expressions aka Regex.

new System.Text.RegularExpressions.Regex("\bAccount:[^a-zA-Z]*\b").Match(strTextFileContent).ToString

Inside an assign will return you the area you want to.

Basically, it will look from the String “Account:” and take all the non Alphabetic characters until it will encounter another one. In your case that would do.

Afterward you could use for example this

new System.Text.RegularExpressions.Regex("’\d{2,7}’").Matches(strAccounts)

This would return you collection of Regex.Match. You then just have to loop trough it to find all the accounts or push them to an array/datatable/list assuming that they are surrounded by single quotation marks and are between 2 and 7 digits.



Thanks Florent

In the end I went old school

I added an additional variable:

Afterwards, I’m checking if the row contains inverted comas (’) and not colon (: )

Then I update the Account

Similarly for the other fields.

Actions when email received