C命令の前にコメントを見つけるには、Rubyの正規表現が必要です。
たとえば、私はこのファイルを持っていますexample.c
/*
* COMMENT NUMBER 1
*/
x = rb_define_class_under (foo, "MyClassName1", bar);
/*
* COMMENT NUMBER 2
*/
y = rb_define_class_under (foo, "MyClassName2", bar);
/*
* COMMENT NUMBER 3
*/
z = rb_define_class_under (foo, "MyClassName3", bar);
次に、パーサーを次のようにルビーに入れparser.rb
ます。
content = File.open('example.c').read
if content =~ /((?>\/\*.*?\*\/))([\w\.\s]+\s=\s)?rb_define_class_under.*?"(MyClassName1)"/m
puts "Comment number 1 is:"
puts $1
end
if content =~ /((?>\/\*.*?\*\/))([\w\.\s]+\s=\s)?rb_define_class_under.*?"(MyClassName2)"/m
puts "Comment number 2 is:"
puts $1
end
if content =~ /((?>\/\*.*?\*\/))([\w\.\s]+\s=\s)?rb_define_class_under.*?"(MyClassName3)"/m
puts "Comment number 3 is:"
puts $1
end
今私が期待する出力はこれです:
Comment number 1 is:
/*
* COMMENT NUMBER 1
*/
Comment number 2 is:
/*
* COMMENT NUMBER 2
*/
Comment number 3 is:
/*
* COMMENT NUMBER 3
*/
しかし、私は得ます:
Comment number 1 is:
/*
* COMMENT NUMBER 1
*/
Comment number 2 is:
/*
* COMMENT NUMBER 1
*/
Comment number 3 is:
/*
* COMMENT NUMBER 1
*/
何か案が?期待される出力を得るための正しい正規表現は何ですか?