Search code examples
regexregex-groupregex-greedy

regex to extract a string between 5th and 6th colon without space


I am working on a regex to extract the string between 5th and 6th string. I need to extract it without space as space is in between colons. Below is a sample from which I have to extract

Abc Cloud : Xyz : Windows : Non Prod : Silver : ATC123XYZ : AQW Service is Down

I have tried : (.+?): however it returns : Xyz :,: Non Prod : and : ATC123XYZ :. What I want is only ATC123XYZ.


Solution

  • You may try:

    ^(?:[^:]+\s*:\s*){5}([^:\s]+)
    

    Demo

    Explanation:

    ^                 from the start of the input
    (?:               match (but don't capture)
        [^:]+\s*:\s*  any term followed by :
    ){5}              match exactly 5 of these
    ([^:\s]+)         then match AND capture the 6th term
    

    The term you want would be available in the first capture group.