gpt4 book ai didi

regex - 如何编写以特定单词匹配开头并一直持续到遇到句点的正则表达式

转载 作者:行者123 更新时间:2023-12-01 09:09:53 24 4
gpt4 key购买 nike

我试图编写一个正则表达式来匹配特定单词并继续匹配直到遇到句点。例如:

文本:

Which Have Never Won Two Races Other Than Maiden, Claiming, Or Starter Or Which Have Never Won Three Races Or Optional Claiming Price Of $25,000. Three Year Olds, 118 Lbs.; Older, 123 Lbs. Non-winners of $24,000 since March 9 Allowed 2 Lbs.

期望的匹配:

Which Have Never Won Two Races Other Than Maiden, Claiming, Or Starter Or Which Have Never Won Three Races Or Optional Claiming Price Of $25,000.

正则表达式:

(?<Match1>Which\s[a-zA-Z]*[.])

不幸的是,我提议的表达式捕获了所有文本,并且似乎延伸到最后一个句点而不是第一个句点。`

最佳答案

其实很简单。使用 [^.]* 匹配所有字符,但不包括下一个 . 并用 \b 边界包围您想要的单词。我将最后的 . 附加为 \. 假设您想将其包含在匹配的输出中。

(?<Match1>\bWhich\b[^.]*\.)

关于regex - 如何编写以特定单词匹配开头并一直持续到遇到句点的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12700880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com