Search code examples
groovygroovy-console

Groovy regex for any string between two fields


I am using groovy regex: notification.ietf-restconf:notification.my-pma-device-notification:device-notification.device-notification.notification.~/.[A-Za-z]$/.entity-ref* for any string between notification and entity-ref but its not working.

What should be the regex for any string to exist between these two fields (groovy)?


Solution

  • There are a few 'notification's in the string, assuming you mean the final one 'device-notification.notification'?

    The following will capture at least one character between the strings.

    def str = '''device-notification.device-notification.notification.CONTAINER-NAME.entity-ref'''  
    
    def group = ( str =~ /(?<=device-notification\.notification\.)(.+)(?=\.entity-ref)/ ) 
    
    assert 1 == group.count
    assert 'CONTAINER-NAME' == group[0][0]​