gpt4 book ai didi

regex - 如何编写正则表达式来匹配标题案例句子(例如 : I Love To Work)

转载 作者:行者123 更新时间:2023-12-05 00:19:09 26 4
gpt4 key购买 nike

我需要找到一个正则表达式来匹配每个句子,无论它是否在标题案例之后(句子中每个单词的第一个字母应该大写,并且单词也可以包含特殊字符)。

最佳答案

regex101

([A-Z][^\s]*)

Regular expression visualization

Debuggex Demo

说明
1st Capturing group ([A-Z][^\s]*)  
[A-Z] match a single character present in the list below
A-Z a single character in the range between A and Z (case sensitive)
[^\s]* match a single character not present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\s match any white space character [\r\n\t\f ]
g modifier: global. All matches (don't return on first match)

Full Sentence
^(?:[A-Z][^\s]*\s?)+$

Regular expression visualization

Debuggex Demo

说明
^ assert position at start of the string
(?:[A-Z][^\s]*\s?)+ Non-capturing group
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
[A-Z] match a single character present in the list below
A-Z a single character in the range between A and Z (case sensitive)
[^\s]* match a single character not present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\s match any white space character [\r\n\t\f ]
\s? match any white space character [\r\n\t\f ]
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string

关于regex - 如何编写正则表达式来匹配标题案例句子(例如 : I Love To Work),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553272/

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