Regex match for first occurrence

633 Views Asked by At

I want start my starts with "["(square bracket). After that I need to find constant string which is "FIELDS THROWING ERROR =>"(constant string), the string will occur after some lines in string. Next, I need to take one word(the word will be dynamic) after constant string then I have to stop after successfully matches the pattern.

sample string: 

------------------------------------------------
Start Method SYNC DATA :: xxx : 5/19/2022 11:09:28 PM : Total Sync Time : 0.00
----------------------------------------------
[xxx][xxx] Upsert Failed : 
     RECORD NUMBER => ABC:000000
     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
     FIELDS THROWING ERROR => Bilcntry
[xxx][xxx] Upsert Failed : 
    RECORD NUMBER => ABC:000000
    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
    FIELDS THROWING ERROR => Bilcntry
[xxx][xxx] Upsert Failed : 
    RECORD NUMBER => ABC:000000
    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
    FIELDS THROWING ERROR => Bilcntry
[xxx][xxx] Upsert Failed : 
    RECORD NUMBER => ABC:000000
    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
    FIELDS THROWING ERROR => Bilcntry

Desire Output:

[xxx][xxx] Upsert Failed : 
     RECORD NUMBER => ABC:000000
     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
     FIELDS THROWING ERROR => Bilcntry

Can anyone help me?

Thanks

3

There are 3 best solutions below

1
On BEST ANSWER
^\[.*([A-Z\s]*=>[ A-Za-z0-9\:\[\]\-\_\,\.\?\'\/\“\”\"\(\)\;\!\@\#\$\%\^\&\*\{\}\|\\\+]*)*

It will work after matches specific word after some lines. I attached output image below. Thanks. enter image description here

2
On

I don't know if it helps with SUMO, but realisation in JAVA could be something like this:

    Pattern pattern = Pattern.compile(
        "^\\[[^]]+]\\[[^]]+][^\\[]+FIELDS THROWING ERROR => (\\w+)$", 
        Pattern.MULTILINE | Pattern.DOTALL);
    Matcher matcher = pattern.matcher("------------------------------------------------\n" +
            "Start Method SYNC DATA :: xxx : 5/19/2022 11:09:28 PM : Total Sync Time : 0.00\n" +
            "----------------------------------------------\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "     RECORD NUMBER => ABC:000000\n" +
            "     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "     FIELDS THROWING ERROR => Bilcntry\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "    RECORD NUMBER => ABC:000000\n" +
            "    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "    FIELDS THROWING ERROR => Bilcntry\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "    RECORD NUMBER => ABC:000000\n" +
            "    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "    FIELDS THROWING ERROR => Bilcntry\n" +
            "[xxx][xxx] Upsert Failed : \n" +
            "    RECORD NUMBER => ABC:000000\n" +
            "    ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry\n" +
            "    FIELDS THROWING ERROR => Bilcntry\n");
    matcher.find();
    System.out.println("entire match = " + matcher.group(0));
    System.out.println("just the dynamic word = " + matcher.group(1));

Note that I have used MULTILINE and DOTALL flags, these are key to getting the entire match in a way you need it. And to extract just that dynamic word, you need to group it somehow (in JAVA regex it is done with brackets, maybe SUMO is similar) Also, i assume that there is no '[' in the message anywhere else except in two places in the start of first maching line.

Output would be:

entire match = [xxx][xxx] Upsert Failed : 
     RECORD NUMBER => ABC:000000
     ERROR MESSAGE => There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries.: Bilcntry
     FIELDS THROWING ERROR => Bilcntry
just the dynamic word = Bilcntry
0
On

This may be help as this will match the first occurrence of given patterns.

\[.*]\[.*] .*: \n(?<message>(.|\n)*?) FIELDS THROWING ERROR => (\w.*)

The results is like this enter image description here