'How to get all values between quotation mark with quotation marks inside value in regex?

I'm trying to get every value in quotes using regex, in some situations I get it and others I don't.

My Regex: /(?:-reason|-r) ?"(.+?)(?:"-|" -|"?$\B)/g

Below are some sentences I've been testing, line 1 and 2 match regex, but line 3 doesn't.

1 anything -reason "some reason" -anyArg "anything"
2 anything -reason "Hello World, you're the "best"" -anyArg "anything"
3 anything -r "anything "here" is wrong" anything

The last sentence of the image is not compatible with the regex, what do I do to make the regex match them all?



Solution 1:[1]

Use

-r(?:eason)?\s*"(.+?)(?:"\s*-|[^"]*$)

See regex proof.

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  -r                       '-r'
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    eason                    'eason'
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    .+?                      any character except \n (1 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    -                        '-'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of grouping

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryszard Czech