私はphpコメントブロックに一致するようにルビーで正規表現を見つけようとしています:
/**
* @file
* lorum ipsum
*
* @author ME <me@localhost>
* @version 00:00 00-00-0000
*/
私が見つけたいくつかの正規表現は正規表現テスターで機能しましたが、ルビーファイルに書いたときには機能しませんでした。
これは、私が見つけた最も成功した正規表現です。
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)
これは私のスクリプトからの出力です
file is ./test/123.rb so regex is ((^\s*#\s)+(.*?))+
i = 0
found: my first ruby comment
file is ./test/abc.php so regex is (/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)
i = 0
found: *
i = 1
found: *
これを行う必要があるコードは次のとおりです。
56 def self.extract_comments f
57 if @regex[File.extname(f)]
58 puts "file is " + f + " so regex is " + @regex[File.extname(f)]
59 cur_rgx = Regexp.new @regex[File.extname(f)]
60 matches = IO.read( f ).scan( cur_rgx )
61 content = ""
62 if ! matches.empty?
63 # content = "== " + f + " ==\n"
64 content += f + "\n"
65 for i in 0...f.length
66 content += "="
67 end
68 content += "\n"
69 for i in 0...matches.length
70 puts "i = " + i.to_s
71 puts "found: " + matches[i][2].to_s
72 content << matches[i][2].to_s + "\n"
73 end
74 content << "\n"
75 end
76 end
77 content || '' # return something
78 end