-1

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?

  • 1
    Does this answer your question? [RegEx: Grabbing values between quotation marks](https://stackoverflow.com/questions/171480/regex-grabbing-values-between-quotation-marks) – Nick Apr 03 '22 at 02:50
  • Please don't use pictures of code and data. For one, readers cannot cut-and-paste in constructing answers. – Cary Swoveland Apr 03 '22 at 03:58
  • @Nick I believe the "solution" you reference requires that internal quotes be escaped, e.g. `"abc\"def\"ghi"`. See [this](https://regex101.com/r/OgoUlQ/1) and [this](https://regex101.com/r/WmuOCs/1). – Booboo Apr 03 '22 at 11:44
  • @Booboo that's correct - but I don't see that there are any internal quotes in OPs data, although it's hard to be 100% certain since they have not indicated their expected result from line 3. – Nick Apr 03 '22 at 12:17
  • @Nick Look at Sentence 3 above: **anything -r "anything "here" is wrong" anything** (presumably the match wanted is **"anything "here" is wrong"**). But then why is Sentence 2. not a problem, also? – Booboo Apr 03 '22 at 14:00
  • You need to specify for each sentence what exactly the expected match should be. – Booboo Apr 03 '22 at 14:09

1 Answers1

0

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
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37