如何在Regex中的引号中获得带有引号内部值的所有值?

发布于 2025-01-18 11:24:31 字数 429 浏览 2 评论 0原文

我试图使用正则表达式获取引号中的每个值,在某些情况下我得到它,而在其他情况下我没有。

我的正则表达式:/(?:-reason|-r) ?"(.+?)(?:"-|" -|"?$\B)/g

下面是我测试过的一些句子,第 1 行和第 2 行与正则表达式匹配,但第 3 行不匹配。

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

图像的最后一句与正则表达式不兼容,我该怎么做才能使正则表达式全部匹配?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2025-01-25 11:24:31

使用

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

请参见 Regex Prace

解释

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文