免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5502 | 回复: 19
打印 上一主题 下一主题

[文本处理] 如何匹配这个表达式? [复制链接]

论坛徽章:
2
狮子座
日期:2014-05-30 21:43:03巳蛇
日期:2014-05-30 21:46:14
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-05-30 13:10 |只看该作者 |倒序浏览
20可用积分
本帖最后由 tgwz88 于 2014-05-30 14:21 编辑

<div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">

我想把上面这个字符串匹配出来,通过sed s///g 或者awk的gsub 进行替换。
可我怎么也匹配不出来,有什么好方法吗?

详情可参见9楼

最佳答案

查看完整内容

回复 14# tgwz88 其实用sed就可以了,这样在时间后面加#会容易点。

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
2 [报告]
发表于 2014-05-30 13:10 |只看该作者
回复 14# tgwz88
其实用sed就可以了,这样在时间后面加#会容易点。
  1. sed -r 's/<[^>]*>//g;s/\s*&nbsp;\s*/\n/g;s/\n+/\n/g;s/\n[0-9]{2}:[0-9]{2}/&#/g' i
  2. May 30, 2014
  3. 05:00#Russia political and economic calendar: May 30
  4. May 29, 2014
  5. 23:09#Poroshenko discusses help to Ukraine with EBRD chief - statement
  6. 22:50#Donetsk militia sends 34 Russian volunteers' bodies to Russia - leader
  7. 22:39#Abkhazia parliament fires PM, urges president to go
  8. 22:16#EEU to be economically, politically attractive for some states - Federation Council (Part 2)
  9. 22:05#Abkhazia parliament to consider dismissal of president - opposition leader
  10. 21:57#Abkhaz opposition ready to discuss all issues related to Ankvab's safety if he resigns voluntarily
  11. 21:57#Arms depot destroyed in attack on Ukraine National Guard base - statement
  12. 21:56#Abkhazia leader denies he is stepping down
  13. 21:56#EEU to be economically, politically attractive for some states - Federation Council
  14. 21:40#ABKHAZ OPPOSITION READY TO DISCUSS ALL ISSUES RELATED TO ANKVAB'S SAFETY IN CASE HE RESIGNS VOLUNTARILY - ABKHAZ NATIONAL COUNCIL MEMBER SHAMBA
  15. 21:37#Businessman shot dead in center of Moscow
  16. 21:36#ABKHAZ PRESIDENT ANKVAB SAYS THAT RUMORS ON HIS RESIGNATION ARE NOT TRUE AND THAT HE CONTINUES CARRYING OUT PRESIDENTIAL DUTIES
  17. 21:12#Abkhaz opposition passes memorandum demanding reforms, "unconditional legal order"
  18. 21:10#Fighting under way in Luhansk region's Aleksandrovsk - Ukrainian army (Part 2)
  19. 20:47#Protestors in central Kharkiv demand Kyiv authorities stop bloodshed
  20. 20:46#Poroshenko, CoE secretary general discuss investigating Maidan, Odessa events
  21. 20:41#Pro-Kyiv volunteers says are forming special battalion
  22. 20:07#Ukraine receives $750 million from World Bank
  23. 20:05#Soldier dies as army truck attacked in east Ukraine - source
  24. 19:58#Dollar, euro gain as Ukraine situation keeps ruble under pressure
  25. 19:52#Main indexes of the Russia stock market for May 29
  26. 19:51#Potanin disposes of 0.1% direct stake in Norilsk Nickel (Part 2)
  27. 19:41#Sisi victory in Egypt presidential poll means closer ties with Russia - lawmaker
  28. 19:39#Donetsk militia detain suspected looters
  29. 19:37#Police arrest suspected rapist, robber operating in Moscow since 1987 - RIC (Part 2)
  30. 19:35#First talks between govt, opposition reps take place in Sukhumi
  31. 19:34#Sibur plans to bid for 17.5% stake in Polief
  32. 19:30#Potanin disposes of 0.1% direct stake in Norilsk Nickel
  33. 19:27#Russian stock market ends Thursday up, unaffected by weak U.S. GDP statistics
  34. Select page:
  35. 1
  36. 2
  37. 3
  38. 4
  39. Next
复制代码

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
3 [报告]
发表于 2014-05-30 13:13 |只看该作者
40px/20px这2个数字么?
regexp好难啊感觉。

grep -o 可以么

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
4 [报告]
发表于 2014-05-30 13:14 |只看该作者
哦字符串啊
[^<[^>]+>]
这个呢?

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
5 [报告]
发表于 2014-05-30 13:19 |只看该作者
这样就好了吧
"^>[^>]+>$"

以^开头,中间若干个非>的字符,最后>结尾。

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
6 [报告]
发表于 2014-05-30 13:28 |只看该作者
此外,真要替换的话,regexp的greedy match不就行了吗

<.*> 会匹配最多的,不知道你的目的是,难道又是X-Y problem ?

论坛徽章:
2
狮子座
日期:2014-05-30 21:43:03巳蛇
日期:2014-05-30 21:46:14
7 [报告]
发表于 2014-05-30 13:33 |只看该作者
回复 4# expert1


    谢谢,可一行文本中有N个这样相同的字符串……,而且这些字符串相邻之间包含如下字符:
data</div><div style=''><a style=''>data

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
8 [报告]
发表于 2014-05-30 13:33 |只看该作者
回复 1# tgwz88
没看懂题目,是从很大的一个html文件中找出类似
  1. <div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">
复制代码
的片段?

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
9 [报告]
发表于 2014-05-30 13:35 |只看该作者
回复 6# tgwz88
能提供一下类似的原始文本数据吗?

   

论坛徽章:
2
狮子座
日期:2014-05-30 21:43:03巳蛇
日期:2014-05-30 21:46:14
10 [报告]
发表于 2014-05-30 13:38 |只看该作者
本帖最后由 tgwz88 于 2014-05-30 20:36 编辑

回复 8# Herowinter

我想根据数据得到如下内容:
May 30, 2014
09:56#Luhansk militia detains 4 Europeans, presumably from OSCE (Part 3)
………等等………


数据如下:
<div style="width: 140px; float: left; height: 20px;"><b style='color: #536570;'>May 30, 2014</b></div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:56</div><div style=''><a href=/newsinf.asp?id=509761 style=''>Luhansk militia detains 4 Europeans, presumably from OSCE (Part 3)</a></div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;color:red;">09:50</div><div style='color:red'>RIGHT SECTOR MILITANTS SUSPECTED OF PLOTTING TERROR ACTS DETAINED IN CRIMEA - RUSSIA'S FSB</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:48</div><div style=''>OSCE Minsk Group co-chairs to hold meetings in Baku</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:41</div><div style=''>TGK-2 negotiating sale of plants with Spring, joint projects with Chinese cos</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:41</div><div style=''>Luhansk militia detains 4 Europeans, presumably from OSCE (Part 2)</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:39</div><div style=''>Abkhaz president reiterates he will not resign or dismiss prime minister</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:37</div><div style=''>Lukoil boosts oil production 1.9% in Q1 to 22.5 mln tonnes, production in West Siberia falls 2.8%</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:34</div><div style=''>Retail lending growth in Russia has slowed to "acceptable" 25-30% - CBR</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:31</div><div style=''>Luhansk militia detains 4 Europeans, presumably from OSCE</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:31</div><div style=''>RusHydro investment program without subsidiaries might exceed plan by 25% in 2015-2017</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:30</div><div style=''>Banks have 1020.8 bln rbs on CBR correspondent accounts on May 30.</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:28</div><div style=''><a href=/newsinf.asp?id=509750 style=''>CBR to auction up to 1.2 trln rubles in 3-month loans backed by nonmarket assets June 9, July 14</a></div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:25</div><div style=''>Luhansk militiamen claim to have "disarmed" Ukraine military unit</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:21</div><div style=''>Moscow press review for May 30, 2014</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:20</div><div style=''>Moscow press review for May 30, 2014</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:20</div><div style=''>Moscow press review for May 30, 2014</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:19</div><div style=''>Moscow press review for May 30, 2014</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:17</div><div style=''>Ankvab's refusal to resign a "silly attempt to cling to power" - Abkhaz opposition</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:13</div><div style=''>7 children injured in Ukraine's Donetsk region fighting between May 9 and May 28</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">09:00</div><div style=''>RUSSIA AND FSU GENERAL NEWS</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">08:35</div><div style=''>Results of forex trading on Moscow Exchange on May 29</div><div style="float: none; clear: left;">&nbsp;</div><div class=nws_date style="width: 40px; float: left; height: 20px;;">05:00</div><div style=''><a href=/newsinf.asp?id=509740 style=''>Russia political and economic calendar: May 30</a></div><div style="float: none; clear: left;">&nbsp;</div>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP