正则表达式匹配句子中的第一个单词 [英] Regex to match first word in sentence

查看:660
本文介绍了正则表达式匹配句子中的第一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个与句子中的第一个单词匹配的正则表达式,不包括标点符号和空格.例如:这是一个句子"中的这".和首先,我想说的是第一!"!

I am looking for a regex that matches first word in a sentence excluding punctuation and white space. For example: "This" in "This is a sentence." and "First" in "First, I would like to say \"Hello!\""

这不起作用:

"""([A-Z].*?(?=^[A-Za-z]))""".r

推荐答案

(?:^|(?:[.!?]\s))(\w+)

将匹配每个句子中的第一个单词.

Will match the first word in every sentence.

http://rubular.com/r/rJtPbvUEwx

这篇关于正则表达式匹配句子中的第一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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