匹配 gcc -L 编译器参数 -- 表达式,仅当它们被撇号包围时才使用空格

Match gcc -L compiler arguments -- Expressions, with whitespaces only if they are surrounded with apostrophes

假设我有一些像下面这样的 gcc 命令行:

gcc main.c -o main -lwhatever -lsomeother -Lpath/to/whatever -L"/path to the/someotherlib"

如何匹配 path/to/whatever"/path to the/someotherlib"(带或不带撇号)?

也许我应该使用一些环顾四周,但我无法弄清楚具体如何。

我开始构建如下所示的表达式

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

然后我添加了撇号:

(?<=-L)"?(.*)"?(?=\s)

原来我不能那样解决问题,因为 .* 匹配任何字符并且在后视中与 \s 以某种方式冲突(也许它消耗它但我认为我什至必须小心用这个声明)。

如果我把[^\s]放在.的位置,那么它不会计算表达式是否在撇号之间,匹配将在第一个空格处结束。

将第2组和第3组合并得到该值

\s-L(?:(["'])((?:(?!).)*)|(\S*))

https://regex101.com/r/uYnUTA/1
https://regex101.com/r/mBp1eH/1

 \s -L                # whitespace then -L param 
 (?:                  # One of these values
    ( ["'] )             # (1), quote delimiter
    (                    # (2 start)
       (?:                  # content within quotes
          (?!  )             # Not a quote
          .                    # this char is ok
       )*                   # do 0 to many
    )                    # (2 end)
                       # backref to quote
  | ( \S* )              # (3), Or, non-quote parameter
 )

请注意,也可以对引号内可能的定界符引号进行任何特殊转义。
只是这里没有完成。