繁体   English   中英

正则表达式匹配前10个单词中的5个或更多大写字母,Python

[英]Regex match for 5 or more Capital letters in First 10 words, Python

如果前10个单词中至少有5个大写字母(也以空格开头),我将创建一个正则表达式以匹配该句子。 我的正则表达式如下:

(^(?:\w+\s(?= [A-Z]{5})){10}.*(?:\n|$))

我的想法是:

^ Match start of string  
?: look for word followed by a boundary i.e a space     
?= Match if Capital letters preceded by a space  
.* - match everything till line end / end string.

我想我需要重组这个,但我不知道该怎么做。 {10}用于前10个字,但放置位置错误。

字符串示例:
比赛- Lets Search For Water somewhere Because I am thirsty and i really am , wishing for a desert rain

不匹配- fully lowercase or maybe One UPPERCASE but there are actually two uppercase letters that are preceded by a space.

您是否被锁定使用正则表达式? 如果不:

# Python 2.7
def checkCaps(text):
  words = text.split()
  caps = 0
  for word in words[:10]:
    if word[0].isupper(): caps += 1
  return caps >= 5

编辑以反映来自@Kevin和@KarlKnechtel的良好反馈(并删除残篇)

在解释器中进行了尝试:

>>> checkCaps('Lets Search For Water somewhere Because I am thirsty and i really am , wishing for a desert rain')
True
>>> checkCaps('fully lowercase or maybe One UPPERCASE but there are actually two uppercase letters that are preceded by a space.')
False

我同意,正则表达式实际上不是为此任务构建的。 您可以寻找一定数量的连续匹配项,但是要使多个匹配项散布在其他匹配项中则很困难,尤其是在您需要保留“其他匹配项”的情况下。

您的任务在概念上围绕单词,因此,将@字符串视为单词的方法(首先将其切成单词)更有意义,如@rchang所示。 使它功能更强大,添加文档并进行更优雅的计数(也可以使用简单的方法,但是这些天我真的不喜欢显式的循环用于“计数”,构建列表等):

def enough_capitalized_words(text, required, limit):
    """Determine if the first `limit` words of the `text`
    contain the `required` number of capitalized words."""
    return sum(
        word[0].isupper()
        for word in text.split()[:limit]
    ) >= required
reduce(lambda count, word: count + word[0].isupper(), text.split()[:10], 0) >= 5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM