0

I'd like to search for all the compilation flags in an expression such as:

-Xaa=2000 -tbb:simple -Xcc-dds -g -c -DC_hh -Xii-ff-file -Xmm-nn -Xkk -o output/gg.o -ee1481 -Xll=0x1000 -I. -I./ss/tt/uu -I./ss/tt/ww -I./EIS_CFG/gentool/make -I./testenv

Using rexex, so that each flag is in its own group. I have come as far as:

(?<= -)(.*?)(?= \-)

which I found in the answers to this question , and it finds everything except the last flag, as it requires each group to have a ' -' after it. How can I also get hold of the last flag?

Edit after I accepted the answer:

Is it possible to include the '-' inside the group?

Also, if the pattern contains a .c / .cpp file to compile, is it possible to remove it?

For example in:

-Xaa=2000 -tbb:simple -Xcc-dds -g -c -DC_hh -Xii-ff-file -Xmm-nn -Xkk -o output/gg.o -ee1481 -Xll=0x1000 -I. ./ff/gg/vv.c -I./ss/tt/uu -I./ss/tt/ww -I./EIS_CFG/gentool/make -I./testenv

Get all groups without the ./ff/gg/vv.c

Carma
  • 15
  • 5

1 Answers1

0

Try this pattern:

(?<= |^)(-\S+)(?= |$)

This pattern says to:

(?<= |^)    assert that what precedes is either a space OR
            what precedes is the very start of the string

(-\S+)      then match and capture everything non whitespace character until

(?= |$)     what follows is another space OR
            what follows is the end of the string

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I edited my question after accepting your answer. Couid you please extend your answer to include the rest of the question? Thanks :) – Carma Jan 27 '19 at 14:45
  • @Carma I updated my answer per your new requirements. Check the demo to see that the regex pattern does not capture the source or output file path. – Tim Biegeleisen Jan 27 '19 at 15:23
  • Thanks! The parameter after -o belongs to the -o even though it's separated by a space. Only a parameter which ends in .c / .cpp should be removed. This is not some random requirement - it's how compilation flags are interpreted by the compiler... – Carma Jan 28 '19 at 11:55
  • I am not updating again. This is the _second_ time you have changed your question. You should ask a new question at this point. Generally speaking, you really need a parser, not a regex, to handle this. – Tim Biegeleisen Jan 28 '19 at 12:04