免费注册 查看新帖 |

Chinaunix

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

[文本处理] confused about []* [复制链接]

论坛徽章:
7
2015年迎新春徽章
日期:2015-03-04 10:01:442015年亚洲杯之科威特
日期:2015-04-17 17:27:042015亚冠之阿尔萨德
日期:2015-07-22 15:25:4015-16赛季CBA联赛之吉林
日期:2016-04-11 13:57:30操作系统版块每日发帖之星
日期:2016-06-24 06:20:00每日论坛发贴之星
日期:2016-06-24 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-05-09 12:31:45
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-08-27 20:59 |只看该作者 |倒序浏览
Sorry I don't have chinese input method in my laptop right now, I have to use english.

Here is the thing:  grep "[0-9]*" file  can match non digital lines such as www.co.com  
If we use: grep "[0-9]" file it only matches the lines contain numbers such as www.co1.com
As we know the regular expression [...] means to match any one of them in [].  Then how to explain [..]*           Why grep "[0-9]*" matches www.co.com?

Thanks!

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
2 [报告]
发表于 2014-08-27 21:31 |只看该作者
* means [0-9] exists 0 time or 1 time or multi times.

论坛徽章:
7
2015年迎新春徽章
日期:2015-03-04 10:01:442015年亚洲杯之科威特
日期:2015-04-17 17:27:042015亚冠之阿尔萨德
日期:2015-07-22 15:25:4015-16赛季CBA联赛之吉林
日期:2016-04-11 13:57:30操作系统版块每日发帖之星
日期:2016-06-24 06:20:00每日论坛发贴之星
日期:2016-06-24 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-05-09 12:31:45
3 [报告]
发表于 2014-08-27 21:42 |只看该作者
Thanks!
Does [ no]* means space or n or o repeats zero or infinite times?
Like grep "can[ no]*t" file matches below lines:
I can not do it

Why? Otherwise [ no]* can mean "space plus no" exists 0 or multiple times?

回复 2# Shell_HAT


   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
4 [报告]
发表于 2014-08-27 22:51 |只看该作者
回复 1# 251744647

>>Why grep "[0-9]*" matches www.co.com?
   
usually the [0-9]* will matches the number, ex: 1, 23, 55,...

but the * (star) have special function with zero digit number
[0-9]* will mathes empty(nothing-- 0 digit number),or 1 digit number, 2 digits number,...

now you have to use [0-9]+ (plus) to do that

[0-9]*  ==> zero or more digits number
[0-9]+ ==> one or more digits number

论坛徽章:
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
5 [报告]
发表于 2014-08-27 22:51 |只看该作者
回复 3# 251744647


   

论坛徽章:
7
2015年迎新春徽章
日期:2015-03-04 10:01:442015年亚洲杯之科威特
日期:2015-04-17 17:27:042015亚冠之阿尔萨德
日期:2015-07-22 15:25:4015-16赛季CBA联赛之吉林
日期:2016-04-11 13:57:30操作系统版块每日发帖之星
日期:2016-06-24 06:20:00每日论坛发贴之星
日期:2016-06-24 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-05-09 12:31:45
6 [报告]
发表于 2014-08-28 09:31 |只看该作者
我觉得是这样理解:[...]*中括号里面如果有两个或以上字符外面再加上*的话, 意味着匹配里面任意数目(不仅仅是任意一个)的字符,以任意顺序,匹配任意多次.举例:
grep "can[ no]t" file        didn't match  I can not do it
grep "can[ no]*t" file       matches       I can not do it
grep "can[no ]*t" file       matches       I can not do it
grep "can[on ]*t" file       matches       I can not do it
grep "can[o n]*t" file       matches       I can not do it

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
7 [报告]
发表于 2014-08-28 09:45 |只看该作者
回复 6# 251744647


    现在还有什么问题吗?

论坛徽章:
9
2015亚冠之大阪钢巴
日期:2015-06-04 11:47:30丑牛
日期:2015-01-22 15:49:26巳蛇
日期:2015-01-22 10:11:18巨蟹座
日期:2014-11-20 10:55:03天蝎座
日期:2014-11-16 22:10:26处女座
日期:2014-11-16 11:01:10申猴
日期:2014-09-19 11:12:37双鱼座
日期:2014-07-25 10:09:54程序设计版块每日发帖之星
日期:2015-08-24 06:20:00
8 [报告]
发表于 2014-08-28 09:49 |只看该作者
我去, 高大上的讨论啊

论坛徽章:
7
2015年迎新春徽章
日期:2015-03-04 10:01:442015年亚洲杯之科威特
日期:2015-04-17 17:27:042015亚冠之阿尔萨德
日期:2015-07-22 15:25:4015-16赛季CBA联赛之吉林
日期:2016-04-11 13:57:30操作系统版块每日发帖之星
日期:2016-06-24 06:20:00每日论坛发贴之星
日期:2016-06-24 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-05-09 12:31:45
9 [报告]
发表于 2014-08-28 10:06 |只看该作者
回复 7# Shell_HAT

下面的理解不知道对不对?
    [...]*中括号里面如果有两个或以上字符外面再加上*的话, 意味着匹配里面任意数目(不仅仅是任意一个)的字符,以任意顺序,匹配任意多次.

论坛徽章:
39
双子座
日期:2014-08-06 17:37:19极客徽章
日期:2016-12-07 14:03:402017金鸡报晓
日期:2017-01-10 15:13:292017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之新疆
日期:2017-03-24 16:36:1915-16赛季CBA联赛之江苏
日期:2017-04-26 17:19:08黑曼巴
日期:2018-03-07 18:56:5615-16赛季CBA联赛之八一
日期:2018-03-09 10:44:1015-16赛季CBA联赛之江苏
日期:2018-03-12 15:12:1915-16赛季CBA联赛之青岛
日期:2018-03-16 09:13:0515-16赛季CBA联赛之山东
日期:2018-04-27 18:23:0515-16赛季CBA联赛之新疆
日期:2018-05-04 11:29:30
10 [报告]
发表于 2014-08-28 10:12 |只看该作者
*表示匹配前面的子表达式零次或多次(大于等于0次)。



for  example

grep "o*"     can match  "none"  or "oo" or "ooo"  or even more "o"..



egrep "(co)*"   can match   "none"   or   "co"   or "coco" or even more "co"


egrep "[123]*"   can match "none" or "1" or "2" or "3" or "11" or "12".......



so
user alone these Expressions
[]*
()*
Character*

No significance
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP