正则表达式匹配任何长度超过八个字母的东西,在 Python 中 [英] Regular expression matching anything greater than eight letters in length, in Python

查看:17
本文介绍了正则表达式匹配任何长度超过八个字母的东西,在 Python 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管试图掌握 grep 和相关的 GNU 软件,我还没有接近掌握正则表达式.我确实喜欢它们,但我还是觉得它们有点碍眼.

Despite attempts to master grep and related GNU software, I haven't come close to mastering regular expressions. I do like them, but I find them a bit of an eyesore all the same.

我想这个问题对某些人来说并不难,但我花了几个小时试图弄清楚如何在我最喜欢的书中搜索超过一定长度的单词,最后,想出了一些非常难看的问题代码:

I suppose this question isn't difficult for some, but I've spent hours trying to figure out how to search through my favorite book for words greater than a certain length, and in the end, came up with some really ugly code:

twentyfours = [w for w in vocab if re.search('^........................$', w)]
twentyfives = [w for w in vocab if re.search('^.........................$', w)]
twentysixes = [w for w in vocab if re.search('^..........................$', w)]
twentysevens = [w for w in vocab if re.search('^...........................$', w)]
twentyeights = [w for w in vocab if re.search('^............................$', w)]

... 每个长度的一行,从某个长度一直到另一个长度.

... a line for each length, all the way from a certain length to another one.

我想要的是能够说给我单词中长度超过八个字母的每个单词."我该怎么做?

What I want instead is to be able to say 'give me every word in vocab that's greater than eight letters in length.' How would I do that?

推荐答案

您不需要为此使用正则表达式.

You don't need regex for this.

result = [w for w in vocab if len(w) >= 8]

但如果必须使用正则表达式:

but if regex must be used:

rx = re.compile('^.{8,}$')
#                  ^^^^ {8,} means 8 or more.
result = [w for w in vocab if rx.match(w)]

参见 http://www.regular-expressions.info/repeat.html有关 {a,b} 语法的详细信息.

See http://www.regular-expressions.info/repeat.html for detail on the {a,b} syntax.

这篇关于正则表达式匹配任何长度超过八个字母的东西,在 Python 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆