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?
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)
(?
()(.[^]+))\(?
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}");
Match m = regx.Match( line);
if ( !m.Success)

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home