简体   繁体   English

在Sublime Text 2中搜索包含“Mickey”但不包含“Mouse”的文件

[英]Search Sublime Text 2 for files that contain 'Mickey' but do not contain 'Mouse'

Is it possible to carry out a search in Sublime Text 2 for files that contain 'Mickey' 是否可以在Sublime Text 2中搜索包含'Mickey'文件

but do not contain 'Mouse' . 但不要包含'Mouse'

Regular expressions maybe? 正则表达式可能? But what would they be? 但他们会是什么?

Yes, you could use regex to do this, using the negative lookahead expression 是的,您可以使用正则表达式来执行此操作,使用负前瞻表达式

Open the Search Ctrl-Shift-F 打开搜索Ctrl-Shift-F

Make sure you Search with Regular Expressions Alt-R 确保使用正则表达式Alt-R

Mickey(?! Mouse)

Would return all instances of ' Mickey ' that were not followed by ' Mouse ' 将返回所有未跟随“ Mouse ”的“ Mickey ”实例

NOTE : make sure you include the space before Mouse ! 注意确保在 Mouse 前包含空格

Is the fact that you use this particular text editor really important? 你使用这个特定的文本编辑器真的很重要吗? Personally, I'd just do it using grep. 就个人而言,我只是使用grep来做。 Something like: 就像是:

grep -l Mickey filename | xargs grep -L Mouse

Explanation: 说明:

The -l option means "print the filename(s) that contain a match, not the matching lines". -l选项表示“打印包含匹配的文件名,而不是匹配的行”。

xargs passes these matching filename(s) into the next grep command. xargs将这些匹配的文件名传递给下一个grep命令。

The -L option means "print the filename(s) that don't contain a match, not the matching lines". -L选项表示“打印不包含匹配项的文件名,而不是匹配的行”。

You can then run this command on multiple files, simultaneously - perhaps by adding a -r ("recursive") option to the first grep. 然后,您可以同时在多个文件上运行此命令 - 可能通过向第一个grep添加-r(“递归”)选项。

I tried the regular expression offered by Smandoli , but it actually crashed Sublime Text 2 ! 我尝试了Smandoli提供的正则表达式,但实际上它已经Smandoli Sublime Text 2

Maybe its a problem with one of the Sublime plugins I've added, I simply don't know. 也许这是我添加的Sublime插件之一的问题,我根本就不知道。

The reason I wanted to use Sublime to do the search is that I'm working on a reasonably large project with well over a thousand files. 我想使用Sublime进行搜索的原因是我正在开发一个包含超过一千个文件的相当大的项目。 So I want to search the project for files containing a particular string ('Mickey'). 所以我想在项目中搜索包含特定字符串('Mickey')的文件。 Edit the file if necessary and add the word Mouse. 如有必要,编辑文件并添加单词Mouse。 Then I could 'refresh' the search which would now omit the file I just edited because it no longer fits the search criteria. 然后我可以“刷新”搜索,现在将省略我刚刚编辑的文件,因为它不再符合搜索条件。 Its a reasonable work flow. 这是一个合理的工作流程。

    Note: The strings are not actually Mickey and Mouse, I just used those to illustrate 
the problem. I don't work for Disney :)

Because the regex offered by Smandoli crashed Sublime I also tried the grep command offered by Tom Lord (I'm running on Windows 7 ) but couldn't see a way to only target files with a particular extension ( *.asp ) 因为Smandoli提供的regex崩溃Sublime我也尝试了Tom Lord提供的grep命令(我在Windows 7上运行)但是看不到只能定位具有特定扩展名的文件的方法( *.asp

In the end I wrote a small Powershell script that seems to do the trick: 最后我写了一个小的Powershell脚本,似乎可以解决这个问题:

Get-ChildItem -recurse | Where-Object {$_.FullName -like "*.asp"}
    ForEach-Object { if( ( $(Get-Content $_) |
                            select-string -pattern "Mickey") )
                     { $_.PsPath }} | Get-ChildItem |
    ForEach-Object { if( !( $(Get-Content $_) |
                            select-string -pattern "Mouse") )
                     { $_.PsPath }}

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

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