正则表达式将匹配替换为匹配 +插件

发布于 2024-11-27 20:55:03 字数 579 浏览 0 评论 0原文

我想替换不区分大小写的 mix:

text StmiXx and some more mIxx text

在正则表达式替换之后:

text St<font color="red">miX</font>x and some more <font color="red>mIx</font> text

代码:

$string = "text StmiXx and some more mIxx text";
$pattern = "/mix/i";
$replacement = "<font color=\"red\">match</font>";
echo preg_replace($pattern, $replacement, $string);

如何使用正则表达式来完成此操作?我也想用 (Linux)sed 尝试一下,或者还有一种使用egrep、grep 的方法。

egrep 有一个选项 --color=always 但没有自定义突出显示。

I would like to replace case-insensitive let's say mix:

text StmiXx and some more mIxx text

after regex replacement:

text St<font color="red">miX</font>x and some more <font color="red>mIx</font> text

code:

$string = "text StmiXx and some more mIxx text";
$pattern = "/mix/i";
$replacement = "<font color=\"red\">match</font>";
echo preg_replace($pattern, $replacement, $string);

How can this be done with regex? I also would like to try this with (Linux)sed or is there also a way with egrep, grep.

egrep has an option --color=always but not custom highlight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

情徒 2024-12-04 20:55:03

你很接近!以下是在 PHP 中执行此操作的方法:

$string = "text StmiXx and some more mIxx text";
$pattern = "/(mix)/i";
$replacement = "<font color=\"red\">$1</font>";
echo preg_replace($pattern, $replacement, $string);

在模式中,括号可用于捕获内容(组)。然后您可以通过重复括号的数量来引用该组。在这种情况下,我们只有一组。这就是为什么在替换中我使用 $1 来引用我们模式中唯一的捕获组。

You were close! Here is how you can do it in PHP:

$string = "text StmiXx and some more mIxx text";
$pattern = "/(mix)/i";
$replacement = "<font color=\"red\">$1</font>";
echo preg_replace($pattern, $replacement, $string);

In the pattern the parentheses can be used to capture the content (group). Then you can reference the group by the number of repeated parentheses. In this case we have only one group. That's why in the replacement I used $1 to reference the only capture group we have in our pattern.

情绪失控 2024-12-04 20:55:03
s="text StmiXx and some more mIxx text"
echo "$s" | sed -e 's!\([mM][iI][xX]\)!<font color="red">\1</font>!g'

编辑:

要执行替换并仅打印匹配的行:

sed -n -e 's!\([mM][iI][xX]\)!<font color="red">\1</font>!g;/[mM][iI][xX]/p'

并且要仅获取前 N 行,您可以将其通过管道传输到 head 或:

sed -n -e '1,10p'
s="text StmiXx and some more mIxx text"
echo "$s" | sed -e 's!\([mM][iI][xX]\)!<font color="red">\1</font>!g'

Edit:

To perform the substitution and only print the lines that match:

sed -n -e 's!\([mM][iI][xX]\)!<font color="red">\1</font>!g;/[mM][iI][xX]/p'

And to get only the first N lines, you can pipe this to head or to:

sed -n -e '1,10p'
甜心小果奶 2024-12-04 20:55:03
$string = "text StmiXx and some more mIxx text";
$pattern = "/(mix)/i";
$replacement = "<font color=\"red\">$1</font>";
echo preg_replace($pattern, $replacement, $string);

必须凝视一分钟才能真正弄清楚代码内部是否有问题:)

$1 代表模式在 () 中找到的第一个东西

例如,如果您出于某种原因想将其更改为 m-ix,您可以这样这:

$string = "text StmiXx and some more mIxx text";
$pattern = "/(m)(ix)/i";
$replacement = "<font color=\"red\">$1-$2</font>";
echo preg_replace($pattern, $replacement, $string);
$string = "text StmiXx and some more mIxx text";
$pattern = "/(mix)/i";
$replacement = "<font color=\"red\">$1</font>";
echo preg_replace($pattern, $replacement, $string);

Had to stare a minute to actually figure out if something was wrong inside the code :)

$1 represents the first thing the pattern finds inside ()

For instance, if you want to change it to m-ix for some reason, you could so like this:

$string = "text StmiXx and some more mIxx text";
$pattern = "/(m)(ix)/i";
$replacement = "<font color=\"red\">$1-$2</font>";
echo preg_replace($pattern, $replacement, $string);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文