3

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

Jervis Lin
  • 151
  • 1
  • 3
  • 11

1 Answers1

1

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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • For some reasons, it still highlights both records in Rudeck. Not really sure the root cause of it – Jervis Lin Oct 23 '17 at 20:45
  • just put up the link to Question – Jervis Lin Oct 23 '17 at 20:59
  • @JervisLin No idea, really. It must be a bug, because the docs say this must work. Are you using the latest build? There was some update 10 days ago. – Wiktor Stribiżew Oct 23 '17 at 21:35
  • get a reply from Rundeck support mentioned **the Highlight Output Global Log Filter is applied “per line”, that is , it cannot filter multi line patterns**. I will take this as an answer since it works the way as described in the question. – Jervis Lin Oct 25 '17 at 19:25