如何编写正则表达式以匹配标题案例句(例如:我喜欢工作) [英] How to write a regex to match title case sentence (Ex: I Love To Work)

查看:26
本文介绍了如何编写正则表达式以匹配标题案例句(例如:我喜欢工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

<小时>

说明

第一个捕获组 ([A-Z][^\s]*)[A-Z] 匹配下面列表中的单个字符A-Z A 和 Z 之间范围内的单个字符(区分大小写)[^\s]* 匹配下面列表中不存在的单个字符量词:*在零次和无限次之间,尽可能多次,按需回馈[贪婪]\s 匹配任何空白字符 [\r\n\t\f ]g 修饰符:全局.所有比赛(不要在第一场比赛中返回)

<小时>

说明

^ 断言字符串开头的位置(?:[A-Z][^\s]*\s?)+ 非捕获组量词:+一次和无限次之间,尽可能多次,按需回馈[贪婪][A-Z] 匹配下面列表中的单个字符A-Z A 和 Z 之间范围内的单个字符(区分大小写)[^\s]* 匹配下面列表中不存在的单个字符量词:*在零次和无限次之间,尽可能多次,按需回馈[贪婪]\s 匹配任何空白字符 [\r\n\t\f ]\s?匹配任何空白字符 [\r\n\t\f ]量词: ?在零到一次之间,尽可能多次,按需回馈[贪婪]$ 断言字符串末尾的位置

I need to find a regex to match each sentence whether it's following Title Case or not (first letter of each word of the sentence should be in upper case and the words can can contain special characters as well).

解决方案

regex101

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

Debuggex Demo


Description

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?)+$

Debuggex Demo

Description

^ 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

这篇关于如何编写正则表达式以匹配标题案例句(例如:我喜欢工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆