正则表达式匹配带连字符和/或撇号的单词 [英] Regex to match words with hyphens and/or apostrophes

查看:432
本文介绍了正则表达式匹配带连字符和/或撇号的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个正则表达式来匹配带有连字符和/或撇号的单词。到目前为止,我有:

I was looking for a regex to match words with hyphens and/or apostrophes. So far, I have:

(\w+([-'])(\w+)?[']?(\w+))

这大部分时间都有效,不过如果有撇号,那么连字符,如qu'est-ce,它不匹配。我可以附加更多的选项,但也许还有另一种更有效的方式吗?

and that works most of the time, though if there's a apostrophe and then a hyphen, like "qu'est-ce", it doesn't match. I could append more optionals, though perhaps there's another more efficient way?

我想要匹配的一些例子:Mary's,High-school,'tis,Chambers ',Qu'est-ce。

Some examples of what I'm trying to match: Mary's, High-school, 'tis, Chambers', Qu'est-ce.

推荐答案

使用此模式

(?=\S*['-])([a-zA-Z'-]+)

演示

(?=                 # Look-Ahead
  \S                # <not a whitespace character>
  *                 # (zero or more)(greedy)
  ['-]              # Character in ['-] Character Class
)                   # End of Look-Ahead
(                   # Capturing Group (1)
  [a-zA-Z'-]        # Character in [a-zA-Z'-] Character Class
  +                 # (one or more)(greedy)
)                   # End of Capturing Group (1)

这篇关于正则表达式匹配带连字符和/或撇号的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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