用什么正则表达式替换代码但保持字符串/变量相同?

时间:2022-03-21 13:15:17

I have a section of code like so:

我有一段代码如下:

$this->getUrl('example/page.html')

and I need to replace it to look like the following:

我需要将其替换为如下所示:

$this->getUrl('', array('_direct' => 'example/page.html'))

My code editor (sublime text) has regular expression find and replace, but how do I do this whilst keeping the 'example/page.html' string across all occurrences?

我的代码编辑器(sublime text)有正则表达式查找和替换,但是如何在所有出现时保持'example / page.html'字符串的同时执行此操作?


So substitution are: '', array('_direct' => ' and ) at the end

所以替换是:'',array('_ direct'=>'和)最后

Thanks

2 个解决方案

#1


2  

This should do the job:

这应该做的工作:

  • find what: (\$this->getUrl\()([^)]+)\)
  • 找到什么:(\ $ this-> getUrl \()([^]] +)\)

  • replace: $1'', array('_direct' => $2))
  • 替换:$ 1'',数组('_ direct'=> $ 2))

#2


0  

Press Ctrl-H for invoking replace bar

按Ctrl-H调用替换栏

then

find what: (?<=\$this->getUrl\()([^)]+)
Replace with: '', array('_direct' => $1)

找到什么:(?<= \ $ this-> getUrl \()([^]] +)替换为:'',array('_ direct'=> $ 1)


first find a zero-match-length with look-behind and the capture between parentheses () to special variable $1

首先找到带有后视的零匹配长度和括号()到特殊变量$ 1之间的捕获


用什么正则表达式替换代码但保持字符串/变量相同?

#1


2  

This should do the job:

这应该做的工作:

  • find what: (\$this->getUrl\()([^)]+)\)
  • 找到什么:(\ $ this-> getUrl \()([^]] +)\)

  • replace: $1'', array('_direct' => $2))
  • 替换:$ 1'',数组('_ direct'=> $ 2))

#2


0  

Press Ctrl-H for invoking replace bar

按Ctrl-H调用替换栏

then

find what: (?<=\$this->getUrl\()([^)]+)
Replace with: '', array('_direct' => $1)

找到什么:(?<= \ $ this-> getUrl \()([^]] +)替换为:'',array('_ direct'=> $ 1)


first find a zero-match-length with look-behind and the capture between parentheses () to special variable $1

首先找到带有后视的零匹配长度和括号()到特殊变量$ 1之间的捕获


用什么正则表达式替换代码但保持字符串/变量相同?