简体   繁体   中英

Lua regex to match pattern in makefile

I'm writing a script to automate the mantainment of my makefile. I need a Lua pattern that matches the following lines:

# objects {
objects = build/somefile1.o \
          build/somefile2.o \
          ...
          build/somefileN.o \

# } objects

I tried with %# objects %{[az%.%s%/%\\\\]+%# %} objects but it doesn't seem to work.

I suggest using:

"\n(# objects %b{} objects)"

To make it work for cases when the match is at the start of the string, you need to prepend the string input with a newline . Here, a newline is matched first, then # objects , then a space, then %b{} matches balanced nested curly braces (if any) and then objects is matched.

When running the extraction, the captured part (within (...) ) will be returned with string.gmatch .

See the Lua online demo

s = [[ YOUR_TEXT_HERE ]]
for m in string.gmatch("\n" .. s, "\n(# objects %b{} objects)") do
   print(m)
 end

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