<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>我爱正则表达式 &#187; csv</title>
	<atom:link href="http://iregex.org/blog/tag/csv/feed" rel="self" type="application/rss+xml" />
	<link>http://iregex.org</link>
	<description>原创、翻译、转载关于正则表达式的文章</description>
	<lastBuildDate>Fri, 30 Mar 2012 12:50:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/><atom:link rel="hub" href="http://www.feedsky.com/api/RPC2"/><atom:link rel="hub" href="http://blogsearch.google.com/ping/RPC2"/><atom:link rel="hub" href="http://blog.yodao.com/ping/RPC2"/><atom:link rel="hub" href="http://www.feedsky.com/api/RPC2"/><atom:link rel="hub" href="http://www.xianguo.com/xmlrpc/ping.php"/><atom:link rel="hub" href="http://www.zhuaxia.com/rpc/server.php"/><atom:link rel="hub" href="http://rpc.technorati.com/rpc/ping"/><atom:link rel="hub" href="http://rpc.pingomatic.com/"/>		<item>
		<title>使用正则表达式转换手机联系人名单</title>
		<link>http://iregex.org/blog/convert-contact-format-from-htc-to-nokia.html</link>
		<comments>http://iregex.org/blog/convert-contact-format-from-htc-to-nokia.html#comments</comments>
		<pubDate>Sun, 05 Jul 2009 10:17:51 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[应用]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[手机]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=62</guid>
		<description><![CDATA[今天换了手机，第一件要紧事就是恢复联系人名单。旧手机是HTC c720W，新手机是Nokia 5320XM，其联系人名单格式不一。难道要我一条条输入吗？阿弥陀佛，几百组联系人的信息，手动操作会死人... ]]></description>
			<content:encoded><![CDATA[<p>今天换了手机，第一件要紧事就是恢复联系人名单。旧手机是HTC c720W，新手机是Nokia 5320XM，其联系人名单格式不一。难道要我一条条输入吗？阿弥陀佛，几百组联系人的信息，手动操作会死人的。</p>
<p>我看了一下以前备份的HTC c720W联系人名单，（说到这里，表扬一下自己：勤于备份真是好习惯，万一某天灾难降临，你还有个指望），发现其格式是这样的：</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;contacts</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;oid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-2147483070<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/oid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;location<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/location<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;businessfaxnumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;companyname</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;department</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;email1address</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mobiletelephonenumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;officelocation</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;businesstelephonenumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jobtitle</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hometelephonenumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;email2address</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;email3address</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;home2telephonenumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;homefaxnumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;categories</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;business2telephonenumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;firstname<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SomeBody<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/firstname<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;middlename</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lastname<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1355***9214<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lastname<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;businessaddresspostalcode</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pagernumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;spouse</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cartelephonenumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;assistantname</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;assistanttelephonenumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;children</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;webpage</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;radiotelephonenumber</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileas<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>***<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileas<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;suffix</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;homeaddressstreet</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;homeaddresscity</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;homeaddressstate</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;homeaddresspostalcode</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;homeaddresscountry</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;otheraddressstreet</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;otheraddresscity</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;otheraddressstate</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;otheraddresspostalcode</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;otheraddresscountry</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;businessaddressstreet</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;businessaddresscity</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;businessaddressstate</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;businessaddresscountry</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;anniversary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1899-12-30 00:00:00<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/anniversary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;birthday<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1899-12-30 00:00:00<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/birthday<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/contacts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<p>而诺基亚的通讯录格式是这样的：</p>
<div class="codecolorer-container csv mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><br /><strong>GeSHi Error:</strong> GeSHi could not find the language csv (using path /home/zhasm/www/iregex.org/wp-content/plugins/codecolorer/lib/geshi/) (code 2)<br /></div>
<p>本想偷懒，去找个xml2csv什么的，不过不太好用。还是请“正则表达式”这个老朋友帮忙吧！</p>
<p>分析xml文件，发现只有<br />
mobiletelephonenumber，businesstelephonenumber，hometelephonenumber，firstname，lastname这5个字段是有用的；对应的csv字段名称是：<br />
&#8220;名&#8221;,&#8221;姓&#8221;,&#8221;常用手机&#8221;,&#8221;常用电话&#8221;,&#8221;公司电话&#8221;</p>
<p>其余的字段大可放心删除。</p>
<p>祭出RegexBuddy，写了这样一条正则表达式：</p>
<div class="codecolorer-container python mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">result <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; r<span style="color: #483d8b;">&quot;&quot;&quot;(?smx)(?#&quot;名&quot;,&quot;姓&quot;,&quot;常用手机&quot;,&quot;常用电话&quot;,&quot;公司电话&quot;)<br />
&nbsp; &nbsp; ^<span style="color: #000099; font-weight: bold;">\s</span>*&lt;item&gt;.*?<br />
&nbsp; &nbsp; &lt;mobiletelephonenumber/?&gt;(?P&lt;mobile&gt;[^<span style="color: #000099; font-weight: bold;">\s</span>&lt;]*).*?<br />
&nbsp; &nbsp; &lt;businesstelephonenumber/?&gt;(?P&lt;business&gt;[^<span style="color: #000099; font-weight: bold;">\s</span>&lt;]*).*?<br />
&nbsp; &nbsp; &lt;hometelephonenumber/?&gt;(?P&lt;home&gt;[^<span style="color: #000099; font-weight: bold;">\s</span>&lt;]*).*?<br />
&nbsp; &nbsp; &lt;firstname/?&gt;(?P&lt;first&gt;[^<span style="color: #000099; font-weight: bold;">\s</span>&lt;]*).*?<br />
&nbsp; &nbsp; &lt;lastname/?&gt;(?P&lt;lastname&gt;[^<span style="color: #000099; font-weight: bold;">\s</span>&lt;]*).*?<br />
&nbsp; &nbsp; &lt;/item&gt;&quot;&quot;&quot;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; r<span style="color: #483d8b;">'&quot;<span style="color: #000099; font-weight: bold;">\g</span>&lt;first&gt;&quot;,&quot;<span style="color: #000099; font-weight: bold;">\g</span>&lt;lastname&gt;&quot;,&quot;<span style="color: #000099; font-weight: bold;">\g</span>&lt;mobile&gt;&quot;,&quot;<span style="color: #000099; font-weight: bold;">\g</span>&lt;home&gt;&quot;,&quot;<span style="color: #000099; font-weight: bold;">\g</span>&lt;business&gt;&quot;'</span><span style="color: #66cc66;">,</span> subject<span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>之所以使用python格式来写正则，是因为它支持命名捕获，看起人直观一些。其实整个替换过程是在RegexBuddy中进行的。</p>
<p>替换后，保存为CSV文件，使用诺基亚自带的软件导入，几百个联系人就又重新归位了。大爽。</p>
<p>附图：<br />
<a href="http://iregex.org/blog/convert-contact-format-from-htc-to-nokia.html" target="_blank"><img src="http://i293.photobucket.com/albums/mm60/zhasm/iregex/regex_contact_manager.png" border="0" alt="Photobucket"></a><br />
<font color="#ffffff">25f30c5f</font></p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/convert-contact-format-from-htc-to-nokia.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

