Regex for matching whole words that contain dots

前端 未结 2 1244
长发绾君心
长发绾君心 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."

提交回复
热议问题