Search code examples
regexdynatrace

Regex to get a nth word from a string which is containing different words which are separated by semicolon(;)


I have a string(Operation logging information) which is combination of different words and all are operated by semicolon(;). I want to extract the 23 word which are tell me the customer is eligible for the operation or Not.

I am new to regex, Could you please help me to find out the regex. I am going to use this regex in Dynatrace tool where JAVA regex function will not work

String example 1: 57423543521;314743352134;Source;Tue Apr 02 12:32:44 BST 2019;ELIGIBILITY;ABC345;MyChannel;DUMBRE;1;RR87835;NM;2019-04-02T07:20:00.000Z;VV;;;M;N;XCB234657;;24234SDF4435345;MrNitinDumbre;N;N;DFD4546;Adult;;MYTICKET;4535435435;;657657655;PP;N;Y;;Y;;N;;;Unconfirmed;;;Not Performed;OK;;Not Known

===> the 23rd word in above the string is 'N' (before the 'DFD4546;Adult', marked as BOLD)

String example 2: 435435435435;345435435657;Source;Tue Apr 02 14:42:20 BST 2019;ELIGIBILITY;PQR490;MyChannel;DUMBRE;2;LK345029;CDGPNQ;2019-04-02T21:15:00.000Z;TT;;;W;Y;;N;34243DFGDFG32434;ProfNitin FDGDFDumbre;N;Y;;Adult;;MYTICKET;345435435;;;;N;Y;;Y;;N;Y;;Unconfirmed;;;Cleared;OK;;Not Known

===> the 23rd word in above the string is 'Y' (before the ';Adult'marked as BOLD)

Thanks in advance


Solution

  • You can use this regex to get exactly the 23rd word, and capture it from group1

    (?:([^;]*);){23}
    

    Here the outer non-grouping parenthesis matches zero or more any character except ; using [^;]* followed by a ; and whole of it matching exactly 23 times using {23} quantifier where last match gets captured in group 1 using ([^;]*)

    Demo