regexgccargumentswhitespaceapostrophe

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


Suppose that I have some gcc command line like this one below:

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

How can I match path/to/whatever and "/path to the/someotherlib" (either with or without the apostrophes)?

Probably I should use some lookarounds but I fail to figure out how exactly.

I started to build an expression like the one below

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

Then I added the apostrophes:

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

Turns out I cannot solve the problem that way, because .* matches any character and somehow clashes with \s in the lookbehind (maybe it consumes it but I think I even have to be careful with this statement).

If I put [^\s] in the place of ., then it would not count whether the expression is between apostrophes or not, the match would end at the first whitespace.


Solution

  • The value can be obtained by concantinating groups 2 and 3.

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

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

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

    Note that any special escaping of possible delimiter quotes inside the quote can be done as well.
    Just not done here.