如果句子以大写字母开头并以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]
部分是正确的,但最后匹配标点符号是错误的。
答案 0 :(得分:8)
我让它为自己工作,只是为了澄清或者如果其他人有同样的问题,这就是我的诀窍:
re.match('^[A-Z][^?!.]*[?.!]$', sentence) is not None
<强>说明:强>
^[A-Z]
在哪里寻找资本
'[^?!.]*'
表示开头和结尾之间的所有内容都可以,但包含?
或!
或.
[?.!]$
必须以?
或!
或.
答案 1 :(得分:2)
使用以下正则表达式。
php artisan config:clear
php artisan cache:clear
正则表达式演示:https://regex101.com/r/jpqTQ0/2
^[A-Z][\w\s]+[?.!]$
输出:
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)
答案 2 :(得分:1)
您的正则表达式检查05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41c1dc08)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: java.lang.NoClassDefFoundError: org.apache.poi.util.POILogFactory
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at org.apache.poi.hssf.usermodel.HSSFWorkbook.<clinit>(HSSFWorkbook.java:156)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at com.mindtech.mindcubecanvas.ReadInventory.saveExcelFile(ReadInventory.java:2006)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at com.mindtech.mindcubecanvas.ReadInventory$4$4.onClick(ReadInventory.java:475)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at android.view.View.performClick(View.java:4653)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at android.view.View$PerformClick.run(View.java:19294)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at android.os.Looper.loop(Looper.java:146)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5603)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
05-03 12:32:12.803 27227-27227/com.mindtech.mindcubecanvas W/System.err: at dalvik.system.NativeStart.main(Native Method)
范围内的单个数字。你应该改为:
[A-Z]
将^[A-Z].*[?.!]$
更改为大写字母与字符串末尾标点符号之间匹配的内容。