How to convert this into Regex?

This is my code

 int i = 0; int lineCount = File.ReadLines("Z:/Daniel/SortedAccounts.txt").Count(); do { string accounts = File.ReadLines("Z:/Daniel/SortedAccounts.txt").Skip(i).Take(1).First(); //First Name int pFrom1 = accounts.IndexOf("#1#") + "#1#".Length; int pTo1 = accounts.LastIndexOf("#2#"); String accountFirstName = accounts.Substring(pFrom1, pTo1 - pFrom1); //Last Name int pFrom2 = accounts.IndexOf("#2#") + "#2#".Length; int pTo2 = accounts.LastIndexOf("#3#"); String accountLastName = accounts.Substring(pFrom2, pTo2 - pFrom2); //Email int pFrom3 = accounts.IndexOf("#3#") + "#3#".Length; int pTo3 = accounts.LastIndexOf("#4#"); String accountEmail = accounts.Substring(pFrom3, pTo3 - pFrom3); //Phone Number int pFrom4 = accounts.IndexOf("#4#") + "#4#".Length; int pTo4 = accounts.LastIndexOf("#5#"); String accountNumber = accounts.Substring(pFrom4, pTo4 - pFrom4); //Preferred Contact int pFrom5 = accounts.IndexOf("#5#") + "#5#".Length; int pTo5 = accounts.LastIndexOf("#6#"); String accountPreferredContact = accounts.Substring(pFrom5, pTo5 - pFrom5); //Populate Combobox accountComboBox.Items.Add(accountLastName + "," + accountFirstName); i = i + 1; } while (i < lineCount); 

I tried converting it to regex but I get an error about out of bounds.

How can I fix my code?

http://ift.tt/1npniPw

by Dgameman1 via /r/csharp

Leave a Reply