开发者

TextMate save result of regex search into new window

开发者 https://www.devze.com 2023-01-24 15:40 出处:网络
I\'d like to be able to perform a search within TextMate, but co开发者_Go百科py the resulting matches into a separate file.

I'd like to be able to perform a search within TextMate, but co开发者_Go百科py 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 (another@example.com)

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.


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


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.


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, ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号