正则表达式匹配并替换由某些字符分隔的单词

发布于 2024-12-05 07:36:30 字数 400 浏览 0 评论 0原文

我需要一些正则表达式的帮助来匹配和替换

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生不灭 2024-12-12 07:36:30

试试这个

$input = "MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH";

echo($input."<br/>");
$result = preg_replace("/
                      (?:^             # Match the start of the string
                      |(?<=[-,.?! ]))  # OR look if there is one of these chars before
                      match            # The searched word
                      (?=([-,.?! ]|$)) # look that one of those chars or the end of the line is following
                      /imx",           # Case independent, multiline and extended
                      "WORD", $input);
echo($result);

Try this

$input = "MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH";

echo($input."<br/>");
$result = preg_replace("/
                      (?:^             # Match the start of the string
                      |(?<=[-,.?! ]))  # OR look if there is one of these chars before
                      match            # The searched word
                      (?=([-,.?! ]|$)) # look that one of those chars or the end of the line is following
                      /imx",           # Case independent, multiline and extended
                      "WORD", $input);
echo($result);
只为一人 2024-12-12 07:36:30

这并不完全符合您的要求,但可能更好地满足您的实际要求(我猜测是“仅当 MATCH”时“将 MATCH 替换为 WORD 是一个完整的单词,而不是另一个单词的一部分”):

$input = 'MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH'
$result = preg_replace('/\bMATCH\b/i', "WORD", $input)

\b 是单词边界锚,仅在字母数字单词的开头或结尾匹配。

结果:

WORD me, yes please,WORD me but dontMATCHme!WORD me and of course WORD, and finally WORD

This is not doing exactly what you asked for, but possibly fulfills your actual requirements better (which I'm guessing to be "Replace MATCH with WORD only if MATCH is an entire word, and not part of a different word"):

$input = 'MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH'
$result = preg_replace('/\bMATCH\b/i', "WORD", $input)

The \b are word boundary anchors that only match at the start or end of an alphanumeric word.

Result:

WORD me, yes please,WORD me but dontMATCHme!WORD me and of course WORD, and finally WORD
巾帼英雄 2024-12-12 07:36:30

这是 PHP 中的一个实现,它将完成您所描述的任务。它将用“WORD”替换所有单词。

<?php

$msg = "MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH";

echo($msg."<br/><br/>");
$msg = preg_replace("/(\w)+/", "WORD", $msg);
echo($msg."<br/>");

?>

Here is an implementation in PHP that will do the task you described. It will replace all words with "WORD".

<?php

$msg = "MATCH me, yes please,MATCH me but dontMATCHme!MATCH me and of course MATCH, and finally MATCH";

echo($msg."<br/><br/>");
$msg = preg_replace("/(\w)+/", "WORD", $msg);
echo($msg."<br/>");

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文