Python Regex用于忽略具有两个连续大写字母的句子

时间:2016-10-09 04:21:04

标签: python regex python-3.x

我手头有一个简单的问题就是忽略包含两个或更多连续大写字母和更多语法规则的句子。

问题:根据定义,正则表达式不应与字符串'This is something with two CAPS.'匹配,但它确实匹配。

代码:

''' Check if a given sentence conforms to given grammar rules

    $ Rules
        * Sentence must start with a Uppercase character (e.g. Noun/ I/ We/ He etc.)
        * Then lowercase character follows.
        * There must be spaces between words.
        * Then the sentence must end with a full stop(.) after a word.
        * Two continuous spaces are not allowed.
        * Two continuous upper case characters are not allowed.
        * However the sentence can end after an upper case character.
'''

import re


# Returns true if sentence follows these rules else returns false
def check_sentence(sentence):
    checker = re.compile(r"^((^(?![A-Z][A-Z]+))([A-Z][a-z]+)(\s\w+)+\.$)")
    return checker.match(sentence)

print(check_sentence('This is something with two CAPS.'))

输出:

<_sre.SRE_Match object; span=(0, 32), match='This is something with two CAPS.'>

2 个答案:

答案 0 :(得分:0)

将你的正则表达式写成负数(找到所有错误句子的句子)可能比在正数中更容易。

if(this.privateRequest.equals("login")){

答案 1 :(得分:0)

您的否定前瞻仅适用于正在测试的字符串的开头。

第二捕获小组interface Base{ void generateItem(); } class Derived1 implements Base{ @Override public void generateItem() { System.out.println("generateItem() from Derived1"); } } class Derived2 implements Base{ @Override public void generateItem() { System.out.println("generateItem() from Derived2"); } } class Main { public static void main(String[] args) { List<Base> list = new ArrayList<>(); list.add(new Derived1()); list.add(new Derived2()); list.forEach(Base::generateItem); } }

generateItem() from Derived1 generateItem() from Derived2 在字符串

的开头断言位置

否定前瞻(^(?![A-Z][A-Z]+))

^

(?![A-Z][A-Z]+)