正则表达式匹配包含点整词 [英] Regex for matching whole words that contain dots

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

问题描述

在下面的语句:

我在下午5点就醒了今天

我想匹配的至下午5时。。 这种模式的工作原理:

I want to match the 5 p.m.. This pattern works:

5 p\.m\.

但我希望它仅全字匹配(所以5的PMU将不匹配)。 所以我用这个模式:

But I wanted it to match whole words only (so 5 p.m.us won't be matched). So I used this pattern:

\b5 p\.m\.\b

但这并没有因为点的工作。

But this didn't work because of the dot.

会是怎样解决这个最简单的正则表达式?我可以定义一个零宽度匹配(\ B型),不关心,如果字符是单词字符与否,只有当它是一个空间或字符串结束\开始?

What will be the simplest regex to solve this? Can I define a zero-width match (\b style) that doesn't care if the character is a word character or not, only if it is a space or the end\beginning of the string?

推荐答案

您可以用积极的前瞻和回顾后,这是一个零宽度匹配,做到这一点。一个正则表达式如(小于?= ^ | \ S)5 P \ .M \(= \ s | $?)表示开始的字符串或空间。性格,其次是下午5点其次是什么这是一个空格字符或字符串的结尾。

You can use positive lookahead and lookbehind, which is a zero-width match, to do it. A regex like (?<=^|\s)5 p\.m\.(?=\s|$) means "Start of the string or a space character, followed by '5 p.m.' followed by anything which is a space character or end of string."

这篇关于正则表达式匹配包含点整词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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