设为首页收藏本站

Crossin的编程教室

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

使用正则表达式匹配单词

[复制链接]

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

15#
发表于 2016-11-9 09:01:56 |只看该作者
crossin先生 发表于 2016-11-6 22:02
你这个需求确实不太好写。

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

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

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

14#
发表于 2016-11-6 22:02:37 |只看该作者
Tony 发表于 2016-11-6 08:54
现在可以了,谢谢crossin先生!
看来我还要努力学习正则表达式啊,你说的这个零宽断言,我以前根本没有听 ...

你这个需求确实不太好写。

推荐看看这个 http://deerchao.net/tutorials/regex/regex-1.htm
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

13#
发表于 2016-11-6 08:54:01 |只看该作者
crossin先生 发表于 2016-11-5 22:05
r='(?

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

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

12#
发表于 2016-11-5 22:05:27 |只看该作者
Tony 发表于 2016-11-5 09:25
我是这样写的:结果:demo.txt 的内容是这些:

Ho454w m545any roads must a man walk downBef434ore the ...

r='(?<=\s)[A-z]+(?=\s|$)|^[A-z]+(?=\s|$)'
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

11#
发表于 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

回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

10#
发表于 2016-11-4 14:18:30 |只看该作者
Tony 发表于 2016-11-3 19:18
对,我就想要的是这个。
可是,按照你写的正则表达式,结果还是不行啊。。。 ...
  1. import re
  2. r='(?<=\s)[A-z]+(?=\s|$)|^[A-z]+'
  3. s='''
  4. Ho454w m545any roads must a man walk downBef434ore they call him a man
  5. H767ow m767any s767eas m676ust a wh453ite do4543ve sai45l
  6. Be56fore s565he sle3434eps i343n th434e san453d
  7. Ho343w man565y tim5767es m767ust t78he cann564on ba45lls f656ly
  8. Be686fore the7876y're f787orever ba7878nned
  9. T7567he an75675swer m76y fr656iend is blowing in the wind
  10. The answer is blow .ing in the wi1n3d th345'are
  11. '''
  12. re.findall(r,s)
复制代码
输出
['roads', 'must', 'a', 'man', 'walk', 'they', 'call', 'him', 'a', 'man', 'a', 'is', 'blowing', 'in', 'the', 'wind', 'The', 'answer', 'is', 'blow', 'in', 'the']
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

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

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

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

8#
发表于 2016-11-2 22:49:28 |只看该作者
Tony 发表于 2016-11-2 19:00
是一个 类似于这样的:
Ho454w m545any roads must a man walk downBef434ore they call him a man
H767o ...

符号分割是符合 \b 的定义的,就是单词的边界。
你想要的其实是两边是空格或开头结尾的纯单词,即使是标点分割也不算,是这样吧。

那我觉得比较复杂。我能想到的办法就是用零宽断言:
(?<=\s)[A-z]+(?=\s|$)
然后还要考虑上第一个单词的情况
^[A-z]+
所以并一起就是
(?<=\s)[A-z]+(?=\s|$)|^[A-z]+
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

7

主题

0

好友

127

积分

注册会员

Rank: 2

7#
发表于 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  等也当成了英文单词。
请问,我需要怎样修改,才能正确,谢谢!
回复

使用道具 举报

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

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

GMT+8, 2024-5-20 04:38 , Processed in 0.020820 second(s), 28 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部