What does this regex `str.gsub(/\#{(.*?)}/)` do?

This question already has an answer here:

  • Greedy vs. Reluctant vs. Possessive Quantifiers 7 answers

  • .* is a greedy match, whereas .*? is a non-greedy match. See this link for a quick tutorial on them. Greedy matches will match as much as they can, while non-greedy matches will match as little as they can.

    In this example, the greedy variant grabs everything between the first { and the last } (the last closing brace):

    'start #{this is a match}{and so is this} end'.match(/#{(.*)}/)[1]
    # => "this is a match}{and so is this"
    

    while the non-greedy variant reads as little as it needs to make the match, so it only reads between the first { and the first successive } .

    'start #{this is a match}{and so is this} end'.match(/#{(.*?)}/)[1]
    # => "this is a match"
    
    链接地址: http://www.djcxy.com/p/76898.html

    上一篇: 在第一个字符比赛停止?

    下一篇: 这个正则表达式`str.gsub(/\#{((*?)}/)`做了什么?