Are there named groups in Groovy regex pattern matches?

前端 未结 3 1695
小蘑菇
小蘑菇 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:33

    Alternative:

    def foo = '''(id:(1 2 3)) OR (blubb)
    (id:(4 5 6)) OR (fasel)
    '''
    ​def matcher =  (foo =~ /\(id:\((.+)\)\) OR (.*)/)
    def (whole, bar, baz) = matcher[0]
    assert whole == '(id:(1 2 3)) OR (blubb)'
    assert bar == '1 2 3'
    assert baz == '(blubb)'
    
    (whole, bar, baz) = matcher[1]
    assert whole == '(id:(4 5 6)) OR (fasel)'
    assert bar == '4 5 6'
    assert baz == '(fasel)'
    

提交回复
热议问题