Regex matching only second group

10,658

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.
Share:
10,658
Jervis Lin
Author by

Jervis Lin

Updated on June 07, 2022

Comments

  • Jervis Lin
    Jervis Lin about 2 years

    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