正则表达式:如何替换模式的一部分并引用其中的变量?

发布于 2024-12-27 08:21:28 字数 341 浏览 2 评论 0原文

我想匹配一个模式,替换部分模式,并使用模式中的变量作为替换字符串的一部分。

这是正确的吗?

/s/^((\s+)private\sfunction\s__construct\(\))/(2)def\s__init__

英语:替换任意数量的空格,后跟字符串 "private function __construct()" 具有相同数量的空格和字符串 def __init__。那么,是我的正则表达式不好还是什么?

部分替换

I want to match a pattern, replace part of the pattern, and use a variable within the pattern as part of the replacement string.

Is this correct?

/s/^((\s+)private\sfunction\s__construct\(\))/(2)def\s__init__

In English: Replace any amount of whitespace followed by the string "private function __construct()" with the same amount of whitespace and the string def __init__. So, is my regex bad or what?

partial replace

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

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

发布评论

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

评论(3

被翻牌 2025-01-03 08:21:28

我假设您想在 vi 中替换它

替换所有出现的情况

:s/^\(\s\+\)private function __construct()/\1def __init__/g

替换第一个

:s/^\(\s\+\)private function __construct()/\1def __init__/

对您的模式的一些建议

  • /vi 中用于搜索,请使用 < code>:
  • 你需要在 vi 中转义 ( )
  • 使用 \i 其中 i 是第 x 个捕获组,如 \ 1 \2 返回引用分组模式替换
  • \s 不能在替换文本中使用,
  • 如果要替换所有出现的内容,请使用 ' ' 而不是使用尾随 /g

http://vimregex.com 应该可以帮助您入门。

I presume you want to replace it in vi

Replace all occurrences

:s/^\(\s\+\)private function __construct()/\1def __init__/g

Replace first

:s/^\(\s\+\)private function __construct()/\1def __init__/

Few suggestions to your pattern

  • / is used in vi for search , use :
  • you need to escape ( ) in vi
  • use \i where i is xth capture group like \1 \2 to back reference grouped patterns in replacement
  • \s can not be used in replacement text use ' ' instead
  • use trailing /g if you want to replace all occurrences

http://vimregex.com should help you get started.

孤单情人 2025-01-03 08:21:28

这称为反向引用,您使用 \i 来引用模式中第 i 个捕获的组。

因此,对于模式 ^((\s+)private\sfunction\s__construct\(\)),替换为 \2def __init__

This is called a backreference, and you use \i to refer to the i'th captured group from the pattern.

So for the pattern ^((\s+)private\sfunction\s__construct\(\)), the replacement is \2def __init__.

初见终念 2025-01-03 08:21:28

我认为没有人真正理解这个问题。基本上,我这样做的方式如下:

“如果您想搜索替换模式模式 a,并将其替换为替换字符串模式 i,仅当它以模式模式 b 开头时,然后您需要在替换字符串中包含模式 b,如下所示::/(模式 b)(模式 a)/(模式 b)(i)/g"。

虽然有点啰嗦,但值得一读。

过去,我确信有人会想,“实际上不用 pattern b 替换 pattern b 可以节省大量资源。这样做是多余的”。也许它会自动发生。我还没有在 vi 或任何其他程序中找到内置方法来执行此操作。不过,我确信我可以编写一个脚本来做到这一点。

I don't think anyone really understood the question. Basically, the way I'm doing this is as follows:

"If you want to search for a replacement pattern, pattern a, and replace it with a replacement string, pattern i, only if it starts with a pattern, pattern b, then you need to include pattern b in the replacement string, like this: :/(pattern b)(pattern a)/(pattern b)(i)/g".

It's a little wordy but worth reading.

In the past, I'm sure that someone has thought, "It could save a lot of resources to not actually replace pattern b with pattern b. It's redundant to do so." Maybe it happens automatically. I haven't found a built-in method in vi or any other program to do that. I'm sure I could write a script to do it, though.

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