Regex for matching whole words that contain dots

前端 未结 2 1245
长发绾君心
长发绾君心 2021-01-15 16:15

In the following sentence:

I woke up in 5 p.m. today

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

5 p\\         


        
相关标签:
2条回答
  • 2021-01-15 16:49

    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."

    0 讨论(0)
  • 2021-01-15 16:50

    Well, for that specific example, a simple one would be:

    \b5 p\.m\.\B
    

    But I doubt that would work very well in more complex source texts.

    This article on word boundaries may be of help.

    0 讨论(0)
提交回复
热议问题