2
votes

Is there any way that just match the string of second group by just using regex?

The tool I'm using is Rundeck's Global Log Filters (Hightlight Output)

Tool description:

Regular Expression to test. Use groups to selectively highlight. More Use a non-grouped pattern to highlight entire match:

regex: test message: this is a test result: this is a test Use regex groups to only highlight grouped sections:

regex: this (is) a (test) result: this is a test See the Java Pattern documentation.

Example:

3)  Manager port: 2614
4)  Tcp Port: 2615
3)  Manager port: 2714
4)  Tcp Port: 2715

Above is the strings, I want to get the match of "3) Manager port: 2714" only. Here is what I come up (?:Manager port:.*){1}

But it still matching both 3) Manager port: 2614 and 3) Manager port: 2714

Thanks

1
@WiktorStribiżew Thank you so much. That's exactly what I need.Jervis Lin
If you want only the second line that starts with "3) Manager port: " you shouldn't use regex. a simple String.startsWith() would do the trick.Nir Alfasi
@JervisLin Are you sure it works in your target environment? Please check and let know. It is a PCRE/Onigmo/Boost/PyPi regex pattern only.Wiktor Stribiżew
@WiktorStribiżew Thanks for bringing that up. I just tested it and it didn't work on my environment. Mine is using Java patternJervis Lin

1 Answers

1
votes

The (?:Manager port:.*){1} is equal to Manager port:.* and just matches any Manager: substring and the rest of the line.

With a java.util.regex regex library, you may use

[\s\S]*(Manager port:.*)

See the regex demo

Details

  • [\s\S]* - any 0+ chars as many as possible (note that [\s\S] is a "hack", you may safely use (?s:.*) to do the same thing as (?s:.*) represents a modifier group with a DOTALL modifier turned on and thus . matches any char including line break chars)
  • ( - Capturing group start
  • Manager port: - a literal substring
  • .* - 0+ chars other than line break chars, as many as possible.
  • ) - capturing group end.