Python regex 要在线结束时匹配标点符号

如果优惠以大写字母和结束,我需要比较 [?.!] 在 Python.

编辑

他必须有 [?.!]

只要

在最后,

但要允许其他标点符号在提案中


import re
s = ['This sentence is correct.','This sentence is not correct', 'Something is !wrong! here.','"This is an example of *correct* sentence."']

# What I tried so for is:
for i in s:
print/re.match/'^[A-Z][?.!]$', i/ is not None/


在一些变化之后,它不起作用,我知道那部分
^[A-Z]

它是正确的,但结束时标点符号的对应不正确。
已邀请:

詹大官人

赞同来自:

我为自己做了,只是为了澄清或者别人有同样的问题,这就是诀窍对我所做的:


re.match/'^[A-Z][^?!.]*[?.!]$', sentence/ is not None


解释:

在哪里
^[A-Z]

寻找首都的首都


'[^?!.]*'

means everything in between start and end is ok except things containing
?

or
!

or
.



[?.!]$

must end with
?

or
!

or
.

涵秋

赞同来自:

使用以下内容 regex.


^[A-Z][\w\s]+[?.!]$


Regex 演示:
https://regex101.com/r/jpqTQ0/2

import re
s = ['This sentence is correct.','this sentence does not start with capital','This sentence is not correct']

# What I tried so for is:
for i in s:
print/re.match/'^[A-Z][\w\s]+[?.!]$', i/ is not None/


出口:


True
False
False


http://ideone.com/7DTs5q

涵秋

赞同来自:

你的 regex 检查范围内的一个数字是否存在
[A-Z]

. 您必须更改:


^[A-Z].*[?.!]$


修正案
.*

您想要在线结束时的大写字母和标点符号之间进行比较。

要回复问题请先登录注册