正则表达式匹配Python中句子中每个单词的最小长度

正则表达式匹配Python中句子中每个单词的最小长度,python,regex,Python,Regex,我想写一个正则表达式,检查句子中每个单词的长度,如果所有单词的长度至少为3,则返回True。此外,整个句子必须只有小写字母。例如,对于字符串“hello world”,它必须为字符串“hi world”返回true结果和false结果 下面的正则表达式不能按预期工作,它给出了True bool(re.compile('([a-z\s]{3,})+$).match(“hi-world”)) 我想你不需要正则表达式。你可以这样做: s = 'this is a sentence of some so

我想写一个正则表达式,检查句子中每个单词的长度,如果所有单词的长度至少为3,则返回
True
。此外,整个句子必须只有小写字母。例如,对于字符串“hello world”,它必须为字符串“hi world”返回true结果和false结果

下面的正则表达式不能按预期工作,它给出了
True

bool(re.compile('([a-z\s]{3,})+$).match(“hi-world”))


我想你不需要正则表达式。你可以这样做:

s = 'this is a sentence of some sort'
words = s.split()
test = [w for w in words if len(w) > 3]
print(len(test) == len(words)) # False
或相当于:

s = 'this is a sentence of some sort'
words = s.split()
acceptable = lambda x: len(x) > 3
print(len(words) == len(list(filter(acceptable, words))))
甚至:

s = 'this is a sentence of some sort'
words = s.split()
res = all(len(word) > 3 for word in words)
print(res)
或者,正如@pault所说:

s = 'this is a sentence of some sort'
all(len(w) > 3 and w.islower() for w in s.split())

我想你不需要正则表达式。你可以这样做:

s = 'this is a sentence of some sort'
words = s.split()
test = [w for w in words if len(w) > 3]
print(len(test) == len(words)) # False
或相当于:

s = 'this is a sentence of some sort'
words = s.split()
acceptable = lambda x: len(x) > 3
print(len(words) == len(list(filter(acceptable, words))))
甚至:

s = 'this is a sentence of some sort'
words = s.split()
res = all(len(word) > 3 for word in words)
print(res)
或者,正如@pault所说:

s = 'this is a sentence of some sort'
all(len(w) > 3 and w.islower() for w in s.split())

下面是一种在不使用正则表达式的情况下执行此操作的方法:

def all_words_three_or_more(sentence):
  sentence_list = sentence.split(' ')
  for word in sentence_list:
    if len(word) < 3 or word.lower() != word:
      return False
  return True

下面是一种在不使用正则表达式的情况下执行此操作的方法:

def all_words_three_or_more(sentence):
  sentence_list = sentence.split(' ')
  for word in sentence_list:
    if len(word) < 3 or word.lower() != word:
      return False
  return True
请尝试一下:

import re
pattern = re.compile('([a-z\s]{3,})+$')
all(pattern.match(x) for x in  "hello world".split())
输出:

True
False

输出:

True
False
请尝试一下:

import re
pattern = re.compile('([a-z\s]{3,})+$')
all(pattern.match(x) for x in  "hello world".split())
输出:

True
False

输出:

True
False

如前所述,这可能不是一个需要正则表达式的问题,但问题可能是一个更大问题的简化,其中使用正则表达式是正确的方法

我的解决方案不是检查每个单词是否符合您的要求,而是尝试查找任何不符合您要求的单词。这意味着我们正在寻找:

  • 不是小写字母或空格字符的任何字符
  • 小于最小长度的单词(3)
  • 生成以下正则表达式:

  • [^a-z\s]
  • (^ |\s)[a-z]{1,2}(\s |$)
  • 将这些组合在一起可以得到:
    ([^a-z\s])|((^ |\s)[a-z]{1,2}(\s |$)
    。这提供了以下可用的Python代码:

    import re
    pattern = '([^a-z\s])|((^|\s)[a-z]{1,2}(\s|$))'
    
    result1 = not bool(re.search(pattern, 'hello world'))
    result2 = not bool(re.search(pattern, 'hi world'))
    

    如前所述,这可能不是一个需要正则表达式的问题,但问题可能是一个更大问题的简化,其中使用正则表达式是正确的方法

    我的解决方案不是检查每个单词是否符合您的要求,而是尝试查找任何不符合您要求的单词。这意味着我们正在寻找:

  • 不是小写字母或空格字符的任何字符
  • 小于最小长度的单词(3)
  • 生成以下正则表达式:

  • [^a-z\s]
  • (^ |\s)[a-z]{1,2}(\s |$)
  • 将这些组合在一起可以得到:
    ([^a-z\s])|((^ |\s)[a-z]{1,2}(\s |$)
    。这提供了以下可用的Python代码:

    import re
    pattern = '([^a-z\s])|((^|\s)[a-z]{1,2}(\s|$))'
    
    result1 = not bool(re.search(pattern, 'hello world'))
    result2 = not bool(re.search(pattern, 'hi world'))
    
    你也可以试试这个

    (?m)^(?=\s*([a-z]{3,}\s*)*$).*
    
    你也可以试试这个

    (?m)^(?=\s*([a-z]{3,}\s*)*$).*
    

    ^([a-z]{3,}\s?)+$
    您可以使用
    re.match(r'\s*[a-z]{3,}(?:\s+[a-z]{3,})*\s*$',s)
    您不需要正则表达式-用空格分割句子,并检查所有单词是否至少有3个字符长:
    如果全部(len word>2个用于句子中的单词。split()
    。。。当然,如果你必须用标点符号、连字符等来解释,你会遇到更困难的情况。但是如果没有后期处理,即使是正则表达式也不能很好地处理。@zwer谢谢你的评论。好。。。没有正则表达式我也可以这样做,但我最感兴趣的是正则表达式在这个问题上的功能。
    ^([a-z]{3,}\s?)+$
    你可以使用
    re.match(r'\s*[a-z]{3,}(?:\s+[a-z]{3,})*\s*$,s)
    你不需要正则表达式来解决这个问题-用空格分割你的句子,检查所有单词是否至少有3个字符长:
    如果所有的话(len(word)>2表示句子中的单词。split()
    …当然,如果你必须用标点符号、连字符等进行解释,你会遇到更困难的情况。但即使是正则表达式也无法在没有后期处理的情况下完美完成。@zwer谢谢你的评论。嗯……没有正则表达式我也可以做到,但我最感兴趣的是正则表达式在解决这个问题时的功能。+这不是正则表达式任务1 t他想要一个正则表达式解决方案。2在普通Python中,有一种更有效的方法可以通过使用
    all
    函数进行测试,该函数可以在某个特定单词测试失败时立即短路。或者干脆使用
    all(len(w)=>3,在s.split()中w的w.islower()
    +这不是一个正则表达式任务1 OP需要一个正则表达式解决方案。2在普通Python中有一种更有效的测试方法,使用
    all
    函数,当某个特定单词测试失败时,该函数就会短路。或者只需
    all(len(w)=>3,w.islower()表示s.split()中的w)
    您可以使用
    word.islower()
    而不是
    word.lower()!=word
    您可以使用
    word.islower()
    而不是
    word.lower()!=word