Hans Hans - 1 year ago 79
Groovy Question

Regex which grabs everything between two characters at the end of a line

I'm looking to create a regex which grabs the text between two ":"s but only if it is the "last set", for example:

\--- org.codehaus.groovy.modules.http-builder:http-builder:0.7.1

should return:


It should be noted that it's possible to get something like:

\--- org::codehaus::groovy::modules::http-builder:http-builder:0.7.1

because the input does not necessarily follow conventions (based on the problem at hand) but the required information is ALWAYS in the last two ":"s.

I've tried some of the following (minus the end of line):

1) (?<=\:).*(?=\:)
2) [^(.*:)].*[^(:.*)]
3) :.*: (this was the most successful, although I got the ":"s with the result but there are issues when there is more than one set of ":"s)

Futher information:

  • I need to use Groovy for this

  • I can read it using a stream or a file (in case that matters)

Thanks for reading and any help!

Answer Source

That means:

  • Sequence must start with a :
  • Then start capturing (
  • Capture all characters that are not colons [^:]*
  • End capturing ) ...
  • ... at the next colon :
  • Then there's another sequence of chars [^:]*
  • And after that sequence the line must end $ (no more sequence)

Or if you can use non-greedy matches, you can also use


.* means capture as many characters as possible, while .*? means capture as little characters as possible. Not all regex implementation support that, though.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download