[英]ruby regex for php comment block
我一直在嘗試在ruby中找到正則表達式以匹配php注釋塊:
/**
* @file
* lorum ipsum
*
* @author ME <me@localhost>
* @version 00:00 00-00-0000
*/
任何人都可以幫助我嘗試搜索很多東西,即使我發現的某些正則表達式已在正則表達式測試器中工作,但是當我將其寫入我的ruby文件中卻沒有。
這是我發現的最成功的正則表達式:
(/\*([^*]|[\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
似乎/\\/\\*.*?\\*\\//m
應該可以。 同樣,這實際上是c樣式的注釋塊。
除非注釋塊中的每一行以星號開頭很重要,否則您可能要嘗試使用此正則表達式:
/\/\*(?:[^*]+|\*+(?!\/))*\*\//
編輯:這是一個更嚴格的版本,它將僅匹配完全按照您的示例格式設置的注釋:
/^( *)\/\*\*\n(?:\1 \*(?:[^*\n]|\*(?!\/))*\n)+\1 \*\//
此版本僅與在單獨的行上帶有/**
和*/
的注釋匹配。 可以將/**
縮進任意數量的空格 (但不能縮進其他空格字符),但是其他行必須縮進比/**
行多一個空格。
編輯2:這是另一個版本:
/^([ \t]*)\/\*\*.*?\n(?:^\1 .*?\n)+^\1 \*\//
它允許使用制表符和空格(ew)的混合形式進行縮進,但仍要求所有行都符合/**
的縮進(加上一個空格)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.