zxCoder
V2EX  ›  问与答

关于正则表达式符号的优先级问题?

  •  
  •   zxCoder · Dec 25, 2020 · 1239 views
    This topic created in 2169 days ago, the information mentioned may be changed or developed.
    find({}).forPurpose({"key":"value"})
    

    这个字符串用{.*}匹配,不能匹配到第一组{}吗?

    5 replies  •  2020-12-25 22:35:05 +08:00
    zxCoder
    zxCoder
    OP
    Dec 25, 2020
    1
    改成{.*?}就好了 这是为什么呢?
    ?是 0 次或 1 次 *是 0 次或多次
    lululau
    lululau
    Dec 25, 2020
    2
    量词后面的 ? 不表示 0 或者 1 次匹配,搜下“正则非贪婪匹配”
    xupefei
    xupefei
    Dec 25, 2020 via iPhone
    3
    加了问号是非贪婪,能匹配多短就匹配多短。
    msg7086
    msg7086
    Dec 25, 2020
    4
    因为 }).forPurpose({"key":"value" 也是匹配的。
    你用*,匹配尽可能多次。
    *?是匹配尽可能少。
    zxCoder
    zxCoder
    OP
    Dec 25, 2020
    5
    @lululau
    @xupefei
    @msg7086

    懂了 第一次听说这个
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2238 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 217ms · UTC 00:12 · PVG 08:12 · LAX 16:12 · JFK 19:12
    ♥ Do have faith in what you're doing.