免费注册 查看新帖 |

Chinaunix

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

一个正则问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-09 17:20 |只看该作者 |倒序浏览
大家好,遇到一个正则的问题,实在解决不了,来这里请大神帮忙,
问题是这样的:
一个文本里面有如下行:
ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=`f=`seach_f=
ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=9_0_0_0_3`f=`seach_f=
ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=4`f=7_0_0_0_2`seach_f=
ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=4`f=`seach_f=7_0_0_0_quickDownload

这里有一串 数字_数字_数字_数字_  (数字或者字符) 的串,这里称为问题串,这个串只会出现在u15后面,但是不一定会有,希望达到的效果是u15及前面部分都获取,后面的那串有就获取,没有就不获取。请各位帮忙,谢谢.

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
2 [报告]
发表于 2013-01-09 18:31 |只看该作者
  1. #!user/bin/perl
  2. use 5.016;
  3. use strict;
  4. use warnings;
  5. open FH,'<','dali.txt';
  6. while (<FH>){
  7.    say "$+{name1}=>$+{name2}" if /^(?<name1>.*)u15=(.*)(?<name2>._._._._.)/;  
  8. }
  9. close FH;
复制代码
回复 1# askzhigang


   

论坛徽章:
6
摩羯座
日期:2013-08-24 10:43:10狮子座
日期:2013-08-25 10:27:06天秤座
日期:2013-09-11 20:28:44午马
日期:2014-09-28 16:06:0015-16赛季CBA联赛之八一
日期:2016-12-19 13:55:0515-16赛季CBA联赛之天津
日期:2016-12-20 14:01:23
3 [报告]
发表于 2013-01-09 19:10 |只看该作者
本帖最后由 cao627 于 2013-01-09 19:14 编辑

如果没理解错 楼主的意思是
  1. /(^.*u15=).*((?:\d_){4}.)|(^.*u15=)/
复制代码

论坛徽章:
0
4 [报告]
发表于 2013-01-09 19:57 |只看该作者
刚才试过了,好像两个都不正确
照上面的例子,我要是的是

大家好,遇到一个正则的问题,实在解决不了,来这里请大神帮忙,
问题是这样的:
一个文本里面有如下行:
ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=
ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=9_0_0_0_3
ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=7_0_0_0_2
ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=7_0_0_0_quickDownload

论坛徽章:
6
摩羯座
日期:2013-08-24 10:43:10狮子座
日期:2013-08-25 10:27:06天秤座
日期:2013-09-11 20:28:44午马
日期:2014-09-28 16:06:0015-16赛季CBA联赛之八一
日期:2016-12-19 13:55:0515-16赛季CBA联赛之天津
日期:2016-12-20 14:01:23
5 [报告]
发表于 2013-01-09 20:01 |只看该作者
  1. #!/usr/bin/perl
  2.     use 5.014;
  3.     use strict;
  4.     use warnings;
  5.     open FH,'<','dali.txt';
  6.     while (<FH>){
  7.     /(^.*u15=).*((?:\d_){4}\w+)|(^.*u15=)/;
  8.     say "$1$2" if $1;
  9.     say "$3" if $3;
  10.     }
  11.     close FH;
复制代码

论坛徽章:
0
6 [报告]
发表于 2013-01-09 20:20 |只看该作者
好了,非常感谢。:wink:

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34子鼠
日期:2013-12-14 14:57:19射手座
日期:2014-04-25 21:23:23
7 [报告]
发表于 2013-01-09 20:39 |只看该作者
回复 4# askzhigang
  1. while(<DATA>){
  2.     /(.*?u15=).*?((?:\d*_){4}\w+)/?print "$1$2\n":/(.*?u15=)(?{print "$1\n"})/;
  3. }
  4. __DATA__
  5. ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=`f=`seach_f=
  6. ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=9_0_0_0_3`f=`seach_f=
  7. ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=4`f=7_0_0_0_2`seach_f=
  8. ip=59.175.109.58`sid=c6d978d93ed5943974541a23592e5cfa`eid=17256`app=0`bro=other`u13=search`u14=index`u15=4`f=`seach_f=7_0_0_0_quickDownload
复制代码

论坛徽章:
6
摩羯座
日期:2013-08-24 10:43:10狮子座
日期:2013-08-25 10:27:06天秤座
日期:2013-09-11 20:28:44午马
日期:2014-09-28 16:06:0015-16赛季CBA联赛之八一
日期:2016-12-19 13:55:0515-16赛季CBA联赛之天津
日期:2016-12-20 14:01:23
8 [报告]
发表于 2013-01-09 21:15 |只看该作者
三目操作符 ?  :被用到这里了 学习!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP