gpt4 book ai didi

regex - 检查每个单词的第一个字符是否为大写

转载 作者:行者123 更新时间:2023-12-03 23:58:04 25 4
gpt4 key购买 nike

我想检查字符串中每个单词的第一个字符是否为大写。其他字符应为小写。

例如:

My Lady D'Arbanville => True
My Lady d'arbanville => False
My LADY D'Arbanville => False

我试过了:^[A-Z][a-z ']*$但不工作

最佳答案

使用您显示的示例,请尝试以下正则表达式。

^[A-Z][a-z]+\s+[A-Z][a-z]+\s+[A-Z](?:'[A-Z])?[a-z]+$

Online demo for above regex

说明:为上述添加详细说明。

^[A-Z][a-z]+      ##Checking from starting of value if it starts from capital letter followed by 1 or more small letters here.
\s+ ##Matching 1 or more space occurrences here.
[A-Z][a-z]+ ##Matching 1 occurrence of capital letter followed by 1 or more small letters here.
\s+ ##Matching 1 or more space occurrences here.
[A-Z](?:'[A-Z])? ##Matching single capital letter followed by optional ' capital letter here.
[a-z]+$ ##Matching 1 or more occurrences of small letters till end of value here.

关于regex - 检查每个单词的第一个字符是否为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67792632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com