简体   繁体   English

替换类和崇高文本中第一个函数之间所有出现的$符号

[英]Replace all occurrences of a $ symbol in between class and the first function in sublime text

I did a really stupid mistake after reading about var in PHP7 and I mistakenly thought that I don't need neither var nor public to declare variables inside of a class (outside of it's methods). 在阅读了PHP7中的var后,我犯了一个非常愚蠢的错误,我错误地认为我不需要varpublic可以在类内部(在方法之外)声明变量。 So I removed it from 400+ files and did some refactoring on pretty much all of them. 因此,我从400多个文件中将其删除,并对几乎所有文件进行了重构。 So I can't just revert back, because that would leave me with even more work. 所以我不能只退回去,因为那会给我带来更多的工作。

Now I get a fatal error and I need to add either private or public to all variables declared that way. 现在我收到一个致命错误,我需要将privatepublic变量添加到以这种方式声明的所有变量中。 I'm not sure if any of these classes actually need public variables, probably private should be enough. 我不确定这些类中的任何一个是否真的需要公共变量,也许private就足够了。

But I can't figure out if it's even possible to do in Sublime Text 3. I tried many different combinations of regex, but none of them worked. 但是我不知道在Sublime Text 3中是否有可能。我尝试了多种不同的regex组合,但是它们都不起作用。 I'm guessing that I might need a script to run though all files instead, but maybe I'm overlooking something... 我猜想我可能需要一个脚本来运行所有文件,但是也许我正在忽略某些东西...

So I have something like this: 所以我有这样的事情:

class MyClass {
    $myvar1;
    $myvar2;

    function __construct($myvar1 = NULL, $myvar2 = array()) {
        $this->myvar1 = $myvar1;    
        $this->myvar2 = $myvar2;    
    }
}

And I want to turn it into this: 我想把它变成这样:

class MyClass {
    private $myvar1;
    private $myvar2;

    function __construct($myvar1 = NULL, $myvar2 = array()) {
        $this->myvar1 = $myvar1;    
        $this->myvar2 = $myvar2;    
    }
}

EDIT: Please note that there are no comments (neither C/C++ nor # style) between the class declaration and its first function. 编辑:请注意,在类声明及其第一个函数之间没有注释(C / C ++或#样式)。 Also string notation should not be considered. 另外,不应考虑字符串符号。 The solution should solve specifically this issue, because it seems to be impossible to make it universal for all possible cases. 该解决方案应专门解决此问题,因为似乎不可能使其在所有可能的情况下通用。 It seems that using \\G modifier is the key to solve this particular issue the way I wanted to. 看来使用\\G修饰符是解决我想要的特定问题的关键。 So use the answer with caution. 因此,请谨慎使用答案。

I think I need to replace all occurrences of $ with private $ in between class and the first occurrence of function . 我想我需要在classfunction的第一次出现之间用private $替换所有出现的$ Could it be possible just with Sublime Text 3? Sublime Text 3是否有可能?

So far I fail at even finding all occurences of a pattern that starts with class and ends with the first function . 到目前为止,我什至没有发现所有以class开头,以第一个function结束的模式的出现。 Currently I'm trying to improve the following regex: (?s)class (.*?){.+function . 目前,我正在尝试改进以下正则表达式:( (?s)class (.*?){.+function For some reason the selection ends at the last function, not the first one and I can't figure out how to fix that. 由于某种原因,选择在最后一个函数而不是第一个函数结束,因此我不知道如何解决该问题。 As the worst option I could use this pattern to search through all 400+ files and then edit them manually, but that would be a pain, of course... 作为最差的选择,我可以使用这种模式来搜索所有400多个文件,然后手动进行编辑,但这当然很麻烦...

PS: is it even a good practice to declare them there if I assign them in the __construct method anyway? PS:如果我仍然在__construct方法中将它们声明在那里,甚至将它们声明在那里也是一个好习惯吗?

The following regex will only match variables after the class's opening and will not work after a comment or constant declaration. 以下正则表达式仅在类打开后与变量匹配,并且在注释或常量声明后将不起作用。 See regex in use here 查看正则表达式在这里使用

(?:\bclass \w+\s*{\s*|\G(?!\A)\s*)\K(\$\w+\s*(?:;|=.*;))

For a regex that works around comments and constants as well you can use the following ( see regex in use here ). 对于也可以处理注释和常量的正则表达式,您可以使用以下内容( 请参见此处使用的正则表达式 )。 Note that this, once again, only works for variables at the top of a class: 请注意,这再次仅适用于类顶部的变量:

(?:\bclass \w+\s*{\s*|\G(?!\A)\s*(?:(?://|#).*$|/\*[\s\S]*?\*/|\bconst[^;]*;)\s*)\K(\$\w+\s*(?:;|=.*;))

If variables are declared after the top of the class, your best bet for a quick and dirty solution is to use the whitespace as an indicator of what belongs to the class and what doesn't. 如果在类的顶部之后声明了变量,那么快速而又肮脏的解决方案的最佳选择是将空格用作表示什么属于该类以及什么不属于该类的指示符。 Assuming you use proper indentation of either 1 tab character or 4 space characters, you can use ^(?:\\t| {4})\\K(\\$\\w+) 假设您使用1个制表符或4个空格的正确缩进,则可以使用^(?:\\t| {4})\\K(\\$\\w+)

Replacement: private $1 替换: private $1

  • (?:\\bclass \\w+\\s*{\\s*|\\G(?!\\A)\\s*) Matches either of the following: (?:\\bclass \\w+\\s*{\\s*|\\G(?!\\A)\\s*)匹配以下任意一个:
    • \\bclass \\w+\\s*{\\s*
      • \\b Assert position as a word boundary \\b位置为单词边界
      • class Match this literally class逐字匹配
      • \\w+ Match one or more word characters \\w+匹配一个或多个单词字符
      • \\s*{\\s* Match any number of whitespace, followed by { , followed by any number of whitespace \\s*{\\s*匹配任意数量的空格,后跟{ ,后跟任意数量的空格
    • \\G(?!\\A)\\s* Assert position at the end of the previous match and match any number of whitespace \\G(?!\\A)\\s*在上一个匹配项的末尾声明位置,并匹配任意数量的空格
  • \\K Resets the starting point of the reported match. \\K重置报告的比赛的起点。 Any previously consumed characters are no longer included in the final match 最终比赛中将不再包含任何以前消耗的字符
  • (\\$\\w+\\s*(?:;|=.*;)) Capture the following into capture group 1 (\\$\\w+\\s*(?:;|=.*;))将以下内容捕获到捕获组1中
    • \\$\\w+\\s* Match $ literally, followed by one or more word characters then any number of whitespace \\$\\w+\\s*逐字匹配$ ,后接一个或多个单词字符,然后任意数量的空格
    • (?:;|=.*;) Match either ; (?:;|=.*;)匹配任一; or = followed by any character any number of times and then ; =后跟任意字符多次,然后;

Although accepted answer may fit OP needs, it has much pitfalls. 尽管公认的答案可能符合OP的需求,但仍有很多陷阱。 It doesn't work if properties have comments, or class extends another class or implements an interface or constants are defined between properties or comma separated declarations do exist. 如果属性具有注释,或者类扩展了另一个类,或者实现了一个接口,或者在属性之间定义了常量,或者存在逗号分隔的声明,则无法使用。

You may find this regex more accurate (not bulletproof though): 您可能会发现此正则表达式更准确(但不是防弹):

(?m)function(*COMMIT)(*F)|^\s*\K\$\S+\s*[=;,]

Put private $0 as replacement string. 放入private $0作为替换字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM