正则表达式匹配并替换由某些字符分隔的单词
我需要一些正则表达式的帮助来匹配和替换
<comma|dash|fullstop|questionmark|exclamation mark|space|start-of-string>WORD<comma|dash|fullstop|space|end-of-string>
我需要找到一个特定的单词(不区分大小写),其
前面是:逗号或破折号或句号或问号或感叹号或空格或字符串开头
,后面是:逗号或破折号或句号或问号或感叹号或空格或字符串结尾
测试字符串: 匹配我,是的,请匹配我,但不要匹配我!匹配我,当然匹配,最后匹配
我想用 PHP 中的另一个字符串替换所有匹配,所以我可能需要使用 preg_replace 或其他东西?
I need a little help with regex to match and replace
<comma|dash|fullstop|questionmark|exclamation mark|space|start-of-string>WORD<comma|dash|fullstop|space|end-of-string>
I need to find a specific WORD (case insensitive) which
is preceded by: comma or dash or fullstop or question mark or exclamation mark or space or start-of-string
and is followed by: comma or dash or fullstop or question mark or exclamation mark or space or end-of-string
test string:
MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH
I want to REPLACE all matches with another string in PHP, so i possibly need to use preg_replace or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this
这并不完全符合您的要求,但可能更好地满足您的实际要求(我猜测是“仅当
MATCH”时“将
是一个完整的单词,而不是另一个单词的一部分”):MATCH
替换为WORD
”\b
是单词边界锚,仅在字母数字单词的开头或结尾匹配。结果:
This is not doing exactly what you asked for, but possibly fulfills your actual requirements better (which I'm guessing to be "Replace
MATCH
withWORD
only ifMATCH
is an entire word, and not part of a different word"):The
\b
are word boundary anchors that only match at the start or end of an alphanumeric word.Result:
这是 PHP 中的一个实现,它将完成您所描述的任务。它将用“WORD”替换所有单词。
Here is an implementation in PHP that will do the task you described. It will replace all words with "WORD".