Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Best way to parse fixed length text file
Message
De
28/04/2009 10:37:36
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01396650
Message ID:
01396678
Vues:
90
>I agree - I'm just trying to figure out how it could be done with a RegEx - thanks


Pete,

In that case - see sample code
[ I have avoided the var keyword since I see you are in 2.0]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace GregoryAdam.Test
{
	public static class testegex
	{
		public static void Main()
		{
			//                    1
			//           1234567890121234567890123456712345678901234567812345678901234567890112345678901234567890123456789011234567890
			string s = @"BX00027025  AD               ME                CW                   NO'S ALLOCATED TO KEW TRUNKS   29/03/1994";
			string pattern = "^(.{12})(.{17})(.{18})(.{21})(.{31})(.{10})$";

			MatchCollection matches = Regex.Matches(s, pattern);

			if (matches.Count != 1)
			{
				Console.WriteLine("problem");
			}
			else
			{
				Match match = matches[0];

				GroupCollection groups = match.Groups;

				// groups[0] is the whole line
				// start at 1
				for (int i = 1; i < groups.Count; i++)
				{
					Console.WriteLine("{0}:{1}", i, groups[i].Value);
				}
				Console.ReadLine();
			}
			
		}
	}
}
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform