Experience in C++, C#, Java and Process : Living in Montreal

* What is my technical experience as a software developer

* What I have learned in the software business

* Where I am heading to in my career

* How to process in a project

Wednesday, January 26, 2005

Regular expression in C#

It is time consuming to build a regular expression.
I have a text file as below:

idnameaddressemailfaxpassowrd
example:
000010034981Mr. Ray BabsMontreal Canada Test@glinknet.com5142842903PassWord

How do I parse it? Note it is possible no value for address, email, fax, or password

Here is the regular expression I built ( 2 hours work)

(?\d+)\(?(.[^]+))\(?
()(.[^]+))\(?(.[^]+)())\(?()(.[^]+))\(?(.+()))

It retrives the value to groups defined by "?"
(? ()(.[^]+))

Can you find better expression?
Regex regx = new Regex( @"(?\d+)\(?(.[^]+))\(?
()(.[^]+))\(?(.[^]+)())\(?()(.[^]+))\(?(.+()))", RegexOptions.IgnoreCase RegexOptions.Singleline RegexOptions.IgnorePatternWhitespace RegexOptions.Compiled );
Match m = regx.Match( line);
if ( !m.Success)
{
System.Diagnostics.Debug.WriteLine( line, "Import Text Error");
continue;
}
string id = m.Result( "${id}");
string name = m.Result( "${name}");
string address = m.Result( "${address}");
string email = m.Result( "${email}");
string fax = m.Result( "${fax}");
string pdfcode = m.Result( "${pdfcode}");


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home