简体   繁体   中英

ruby multiline grep in file

I'd like to find all cases where a c++ method doesn't end with "NULL);" (any number of spaces between the NULL and the ) and the ; should still let it match).

my code is:

Dir['**/*'].select { |f| File.file?(f) } .map {
   |file| a=File.open(file).read 
   puts a.grep(/begin((?!NULL\s*\)\s*;).)+;/m)
}

I'd like it to match lines like:

begin

anything in here but a paren any number of spaces semicolon
);

but not match

begin 
somestuff that doesn't matter NULL);

and not match

begin 
somestuff that doesn't matter 
NULL);

The problem is that my code currently only matches single lines.

Rubular for it is here: http://rubular.com/r/Ex9kmQLecH

I ended up doing a hacky solution, I never could get the multiline stuff to work.

Dir['**/*'].select { |f| File.file?(f) } .map {
   |file| a=File.open(file).read 
   a.gsub("\n"," ")
   a.gsub(";",";\n")
   puts a.grep(/begin((?!NULL\s*\)\s*;).)+;/)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM