正则表达式匹配两个字符串之间的所有字符,包括尾

Regex Match all characters between two strings including tail

我想搜索表达式中的所有编译标志,例如:

-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

使用 rexex,使每个标志都在其自己的组中。 我已经达到:

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

我在 this question 的答案中找到了它,它找到了除最后一个标志之外的所有内容,因为它要求每个组后面都有一个“-”。 我怎样才能同时获得最后一面旗帜?

接受答案后编辑:

是否可以在组内包含“-”?

此外,如果模式包含要编译的 .c / .cpp 文件,是否可以将其删除?

例如:

-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

获取没有./ff/gg/vv.c

的所有组

试试这个模式:

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

此模式表示:

(?<= |^)    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