免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4034 | 回复: 4

正则的问题。如何匹配一个单词 [复制链接]

论坛徽章:
0
发表于 2009-07-17 18:32 |显示全部楼层
测试文件内容

  1. This file tests for book in various places, such as
  2. book at the beginning of a line or
  3. at the end of a line book
  4. as well as the plural books and
  5. handbooks.  Here are some
  6. phrases that use the word in different ways:
  7. "book of the year award"
  8. to look for a line with the word "book"
  9. A GREAT book!
  10. A great book? No.
  11. told them about (the books) until it
  12. Here are the books that you requested
  13. Yes, it is a good book for children
  14. amazing that it was called a "harmful book" when
  15. once you get to the end of the book, you can't believe
  16. A well-written regular expression should
  17. avoid matching unrelated words,
  18. such as booky (is that a word?)
  19. and bookish and
  20. bookworm and so on.
复制代码


运行及结果
  1. $ egrep "(^| )[\"[{(]*book[]})\"?\!.,;:'s]*( |$)" bookwords
  2. This file tests for book in various places, such as
  3. book at the beginning of a line or
  4. as well as the plural books and
  5. "book of the year award"
  6. A great book? No.
  7. told them about (the books) until it
  8. Here are the books that you requested
  9. Yes, it is a good book for children
  10. amazing that it was called a "harmful book" when
  11. once you get to the end of the book, you can't believe
复制代码
目的,匹配book这个关键词,包含复数形式(books)
sed&awk 第二版显示,如上正则匹配及结果如下
  1. $ egrep "(^| )[\"[{(]*book[]})\"?\!.,;:'s]*( |$)" bookwords
  2. This file tests for book in various places, such as
  3. book at the beginning of a line or
  4. at the end of a line book
  5. as well as the plural books and
  6. "book of the year award"
  7. to look for a line with the word "book"
  8. A GREAT book!
  9. A great book? No.
  10. told them about (the books) until it
  11. Here are the books that you requested
  12. Yes, it is a good book for children
  13. amazing that it was called a "harmful book" when
  14. once you get to the end of the book, you can't believe
复制代码


不知为啥
A GREAT book!

就是无法匹配到。

谢谢指教

论坛徽章:
0
发表于 2009-07-17 18:38 |显示全部楼层
$ egrep --version
GNU grep 2.5.3

Copyright (C) 1988, 1992-2002, 2004, 2005  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
发表于 2009-07-17 18:48 |显示全部楼层
没有问题啊。

论坛徽章:
0
发表于 2009-07-17 18:50 |显示全部楼层
我这儿也OK……
邪门儿了,刚才那帖也是我这儿没问题,他那儿出错……

论坛徽章:
0
发表于 2009-07-17 19:05 |显示全部楼层
$ egrep --version
egrep (GNU grep) 2.5.1

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

貌似成功了
传说中的RPWT?
之前是在cygwin中测试
现在跑了个虚拟机
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP