Search code examples
regexregex-group

Regex optional alternative search


I'm trying to get some information as groups out of some text using regex. Desired result is out of the text: Please check the Health of the JVM=acq855SW1-srv1 Please check the acq855SW1-srv2 profile.It seems like down to get

  • 1st match: Description as 'Please check the Health of the JVM=acq855SW1-srv1' and JVM 'acq855SW1-srv1'
  • 2nd match: Description as "Please check the acq855SW1-srv2 profile.It seems like down" and JVM 'acq855SW1-srv2'

My regex so far is (?<Description>.*?the (?:JVM=|)(?<JvmName>[^\s]+)(?: profile.*+|)) and I'm trying to avoid the fact that it`s taking also the "Please check the Health" as a match.

Regex

How can I make in a way that it wont stop at "the Health" ?


Solution

  • An easy fix is to differentiate Health from the others adding the constraint that that value must contain a -.

    This can be done changing [^\s]+ to [^\s]+-[^\s]+.

    So the result regex is:

    (?<Description>.*?the (?:JVM=)?(?<JvmName>[^\s]+-[^\s]+)(?: profile.*+|))
    

    enter image description here

    If the - is an assumption that cannot be done, you could do the same with numbers.

    [^\s]+\d[^\s]+