• 请不要在回答技术问题时复制粘贴 AI 生成的内容
guxianbang
V2EX  ›  程序员

如何用正则表达式匹配第二个引号里的内容?

  •  
  •   guxianbang · Jun 20, 2019 · 7402 views
    This topic created in 2695 days ago, the information mentioned may be changed or developed.

    当一个句子中有多对引号的时候,例如:

    Last "week" I "went" to the "theatre"

    正则表达式"[^"]+"只能匹配最后一个引号里的内容:theatre

    我想匹配第二个引号里的内容( went ),该怎么写规则呢?

    18 replies  •  2019-06-20 17:01:20 +08:00
    choury
    choury
    Jun 20, 2019 via Android
    1
    多匹配点,然后别的不捕获
    mooncakejs
    mooncakejs
    Jun 20, 2019
    2
    'Last "week" I "went" to the "theatre"'.match(/"\w+"/g)
    guxianbang
    guxianbang
    OP
    Jun 20, 2019
    3
    @mooncakejs 匹配不到
    clarkyi
    clarkyi
    Jun 20, 2019
    4
    "\w+"$
    clarkyi
    clarkyi
    Jun 20, 2019
    5
    @clarkyi 这一条也不行
    mooncakejs
    mooncakejs
    Jun 20, 2019
    6
    @guxianbang 这是全匹配出来,找最后一个就好了。
    只要最后一个就 'Last "week" I "went" to the "theatre"'.match(/"\w+"$/)
    daxiaolian
    daxiaolian
    PRO
    Jun 20, 2019
    7

    ```
    var re = /"[^"]+"/g;
    var s = 'Last "week" I "went" to the "theatre"';
    var m;

    do {
    m = re.exec(s);
    if (m) {
    console.log(m[0]);
    }
    } while (m);
    ```
    loryyang
    loryyang
    Jun 20, 2019
    8
    re.match(r'[^"]*"[^"]*"[^"]*"([^"]*)".*', 'Last "week" I "went" to the "theatre"').group(1)
    得到 went
    daxiaolian
    daxiaolian
    PRO
    Jun 20, 2019
    9
    @guxianbang 我觉得不是正则的问题,是你获取的不对
    guxianbang
    guxianbang
    OP
    Jun 20, 2019
    10
    谢谢各位🙏,我研究一下
    pkookp8
    pkookp8
    Jun 20, 2019 via Android
    11
    ^.*?".*?".*?"(.*?)"
    \1
    ahaodady
    ahaodady
    Jun 20, 2019
    12
    解题问问,突然想到的一个问题
    ahaodady
    ahaodady
    Jun 20, 2019
    13
    12345

    使用 \d{2}
    只能取到 12、34
    我怎么取 23、45 呢
    ahaodady
    ahaodady
    Jun 20, 2019
    14
    顺便回答题主,你的正则会取出所有引号里的内容,如果确定是最后一项的话,在代码里取最后一条即可。

    如果是编辑器里的话,结尾带个$

    "([^"]+)"$
    azh7138m
    azh7138m
    Jun 20, 2019
    15
    零宽断言?
    ^.*?(?<=".*?").*?"(.*?)"
    先匹配一个"",然后取后面这个""
    zhzbql
    zhzbql
    Jun 20, 2019
    16
    js 解法
    /^.*?".*?".*?"(.*?)"/.exec('Last "week" I "went" to the "theatre"')[1]
    或 'Last "week" I "went" to the "theatre"'.match(/".*?"/g)[1]
    hjq98765
    hjq98765
    Jun 20, 2019
    17
    @ahaodady 零宽断言

    以 python 为例:

    import re
    re.findall('(?=(\d{2}))','12345')
    # ['12', '23', '34', '45']
    ismyyym
    ismyyym
    Jun 20, 2019
    18
    python:

    import re

    str='Last "week" I "went" to the "theatre"'

    x=re.search('^.*?".*?".*?"(.*?)"',str ).group(1) #正数第二个”“

    y=re.search('.*"(.*?)".*?".*?".*?$',str ).group(1) #倒数第二个”“

    z=re.findall('"(.*?)"',str)[1] # 返回所有”“,取第二个


    用对下面这些
    ^ $ *?
    开头 结尾 尽可能少
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1273 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 69ms · UTC 20:40 · PVG 04:40 · LAX 12:40 · JFK 15:40
    ♥ Do have faith in what you're doing.