Are there named groups in Groovy regex pattern matches?

前端 未结 3 1714
小蘑菇
小蘑菇 2020-12-25 14:11

Something like:

def match = \"John 19\" =~ /(&name&)\\w+ (&age&\\d+)/
def name = match.name
def age = match.age

Is there a

3条回答
  •  滥情空心
    2020-12-25 14:23

    This doesn't name the groups, but a closure does parameterise the match:

    ("John 19" =~ /(\w+) (\d+)/).each {match, name, age ->
      println match
      println name
      println age
    }
    

    which outputs:

    John 19
    John
    19
    

    This is a useful reference: http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/

提交回复
热议问题