TextMate 将正则表达式搜索结果保存到新窗口中

发布于 2024-10-01 18:23:08 字数 441 浏览 2 评论 0原文

我希望能够在 TextMate 中执行搜索,但将结果匹配复制到单独的文件中。

我使用以下表达式:

(?<=\()(.*?)(?=\))

来匹配嵌入在文本行括号内的电子邮件地址,所以有些东西例如:

AN 其他 ([电子邮件受保护])

我正在处理的文件有几百个条目,所有条目均以 CR-LF (\n) 分隔。

我希望能够仅将文本的电子邮件段提取到新文件中以供进一步处理。但是,TextMate 中的搜索对话框似乎仅支持替换匹配的文本。我只是想知道是否有某种方法可以实现这一点。

I'd like to be able to perform a search within TextMate, but copy the resulting matches into a separate file.

I'm using the following expression:

(?<=\()(.*?)(?=\))

to match an email address embedded within brackets of a line of text, so something like:

A N Other ([email protected])

The file I'm working from has a few hundred entries, all separated by CR-LF (\n).

I'd like to be able to extract only the email segment of the text, into a new file for further processing. However, the search dialog within TextMate only seems to support replacing matched text. I was just wondering if there was some way to accomplish this.

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

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

发布评论

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

评论(3

不必了 2024-10-08 18:23:09

这是非常旧的,但您可以在 TextMate2 中执行以下操作:

  1. 进行正则表达式搜索。
  2. 单击右侧结果上方搜索框中的齿轮菜单。
  3. 选择引用您想要的内容的“复制”选项。在本例中为“复制匹配零件”。

http://manual.textmate.org/searching.html#search-results

This is super old, but you can do the following in TextMate2:

  1. Do your regex search.
  2. Click the gear menu in the search box above the results to the right.
  3. Select the Copy option that refers to what you want. In this case, "Copy Matching Parts."

http://manual.textmate.org/searching.html#search-results

满天都是小星星 2024-10-08 18:23:09

命令 Bundles >正文>过滤>将匹配行复制到新文档中可能是一个很好的起点。不过,您必须匹配整行,因此构造一个正则表达式来容纳(尽管丢弃)您要提取的实际文本之前和之后的任何内容。

The command Bundles > Text > Filtering > Copy Matching Lines into New Document could be a good starting point. You will have to match full lines though, so construct a regexp to accomodate (although discard) anything before and after the actual text you want to extract.

再见回来 2024-10-08 18:23:09

据我所知,TextMate无法实现您想要实现的目标。但是,正如您已经提到的,您想要处理您的文件,即您应该编写一个简单的脚本来执行此操作。
如果您需要经常解析此类文件,则可能值得为 TextMate 编写一个命令(捆绑包 > 捆绑包编辑器 > 显示捆绑包编辑器 > 新命令)。
如果这是一次性活动,您可能最好从命令行执行所有操作,即使用 grep、sed 和 awk(如果需要)来处理您的文件并将其输出到另一个文件中。
或者使用您选择的脚本语言。在 Ruby 中,它看起来像这样

matches =[]
File.open(ARGV.first, "r") do |file|
  while line = file.gets
    match = file.match(/your_regular_expression_with_grouping/)
    matches << match unless match.nil?
  end
end
# write out matches which are saved in matches as follows:
# matches[i][0] contains original line of text,
# matches[i][1] result of first grouping,
# matches[i][2] result of second grouping, ...

As far as I know, TextMate cannot do what you want to achieve. However, as you already mentioned you want to process your file, i.e. you should write a simple script which does it.
If you need to parse such files very often it may be worth writing a command for TextMate (Bundles > Bundle Editor > Show Bundle Editor > New Command).
If it's a one time activity you are probably better off to do everything from the command line, i.e. use grep, sed and awk (if necessary) to process your file and output it into another one.
Or use the scripting language of your choice. In Ruby it would look something like

matches =[]
File.open(ARGV.first, "r") do |file|
  while line = file.gets
    match = file.match(/your_regular_expression_with_grouping/)
    matches << match unless match.nil?
  end
end
# write out matches which are saved in matches as follows:
# matches[i][0] contains original line of text,
# matches[i][1] result of first grouping,
# matches[i][2] result of second grouping, ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文