Groovy web console

subscribe to the feed Subscribe
to this
site

Test groovy !=~

Published 2 months ago by Joman68
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
// match 'somedata', followed by 0-N instances of ':somedata'...
String regex = /^somedata(:somedata)*$/

// assert matches...
assert "somedata" ==~ regex
assert "somedata:somedata" ==~ regex
assert "somedata:somedata:somedata" ==~ regex

// assert not matches...
try {
   assert !("somedata:somedata:somedata" ==~ regex)
   throw new RuntimeException("Won't get here")
} catch (AssertionError ae) {
  // Expect to get here
}

try {
   assert "somedata:somedata:somedata" !=~ regex
   throw new RuntimeException("Do get here because !=~ doesn't negate the match")
} catch (AssertionError ae) {
  // Won't get here
}