设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 15892|回复: 7
打印 上一主题 下一主题

使用正则表达式匹配单词

[复制链接]

7

主题

0

好友

127

积分

注册会员

Rank: 2

跳转到指定楼层
楼主
发表于 2016-10-30 10:15:34 |显示全部楼层 |倒序浏览
r'\b[a-zA-Z][a-zA-Z]+\b
结果不能匹配:.ing  the7876y're  这类
请问这是怎么回事?谢谢!
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

沙发
发表于 2016-10-31 20:16:31 |显示全部楼层
crossin先生 发表于 2016-10-30 17:18
你这个规则就是匹配两个以上字母的单词,中间有数字是没法匹配的。符号也不会匹配进来 ...

我使用的是\b \b将字母包含在了其中啊,表示开头是字母,接着是一连串字母结尾
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

板凳
发表于 2016-11-1 22:18:54 |显示全部楼层
crossin先生 发表于 2016-11-1 17:12
你现在结果是 img re ,这个有问题吗?你没说清,你说的不能匹配是什么意思。
the和y因为旁边跟的是数 ...

我想我明白先生的意思了,那么,请问,我应该怎样写,才能让其正确呢?
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

地板
发表于 2016-11-2 19:00:53 |显示全部楼层
crossin先生 发表于 2016-11-2 15:39
你要匹配出哪个?你好像还是没有说
你给个例子,什么样的文本,要从中匹配出哪些。你用了什么规则,结果匹 ...

是一个 类似于这样的:
Ho454w m545any roads must a man walk downBef434ore they call him a man
H767ow m767any s767eas m676ust a wh453ite do4543ve sai45l
Be56fore s565he sle3434eps i343n th434e san453d
Ho343w man565y tim5767es m767ust t78he cann564on ba45lls f656ly
Be686fore the7876y're f787orever ba7878nned
T7567he an75675swer m76y fr656iend is blowing in the wind
The answer is blow .ing in the wi1n3d th345'are

现在,我需要用正则表达式找出其中所有的英文单词。
我使用表达式:r'\b[a-zA-Z][a-zA-Z]+\b
结果它将 .ing 'are  等也当成了英文单词。
请问,我需要怎样修改,才能正确,谢谢!
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

5#
发表于 2016-11-3 19:18:25 |显示全部楼层
crossin先生 发表于 2016-11-2 22:49
符号分割是符合 \b 的定义的,就是单词的边界。
你想要的其实是两边是空格或开头结尾的纯单词,即使是标 ...

对,我就想要的是这个。
可是,按照你写的正则表达式,结果还是不行啊。。。
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

6#
发表于 2016-11-5 09:25:47 |显示全部楼层
本帖最后由 Tony 于 2016-11-5 09:30 编辑
crossin先生 发表于 2016-11-4 14:18
输出
['roads', 'must', 'a', 'man', 'walk', 'they', 'call', 'him', 'a', 'man', 'a', 'is', 'blowing' ...

我是这样写的:

  1. import re
  2. PATH = r'C:\Users\Tony\Desktop\Demo\demo.txt'
  3. s= open(PATH)
  4. file_read = s.read()

  5. words = re.findall(r'(?<=\s)[A-z]+(?=\s|$)|^[A-z]+', file_read)
  6. print words
复制代码
结果:
  1. ['Ho', 'roads', 'must', 'a', 'man', 'walk', 'they', 'call', 'him', 'a', 'man', 'a', 'is', 'blowing', 'in', 'the', 'wind', 'The', 'answer', 'is', 'blow', 'in', 'the']
复制代码
demo.txt 的内容是这些:

Ho454w m545any roads must a man walk downBef434ore they call him a man
H767ow m767any s767eas m676ust a wh453ite do4543ve sai45l
Be56fore s565he sle3434eps i343n th434e san453d
Ho343w man565y tim5767es m767ust t78he cann564on ba45lls f656ly
Be686fore the7876y're f787orever ba7878nned
T7567he an75675swer m76y fr656iend is blowing in the wind
The answer is blow .ing in the wi1n3d

回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

7#
发表于 2016-11-6 08:54:01 |显示全部楼层
crossin先生 发表于 2016-11-5 22:05
r='(?

现在可以了,谢谢crossin先生!
看来我还要努力学习正则表达式啊,你说的这个零宽断言,我以前根本没有听说过,涨了见识了!
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

8#
发表于 2016-11-9 09:01:56 |显示全部楼层
crossin先生 发表于 2016-11-6 22:02
你这个需求确实不太好写。

推荐看看这个 http://deerchao.net/tutorials/regex/regex-1.htm

我刚好搜到了,谢谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-13 09:10 , Processed in 0.029960 second(s), 28 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部