Regex Experssion to get last occurance Match to next line

Hello, Below is the Input logs string I’m reading from log file.

2023-04-18 05:39:54 [main] INFO com.abc.crsbatch.controller.Controller - Jobs Selected to Run: FUNDING_TYPE_SOURCING Job
2023-04-18 03:52:25 [main] ERROR com.abc.crsbatch.controller.Controller - Exception occurred while Sourcing type Job : TYPE_SOURCING JOB FAILED
2023-04-18 03:52:25 [main] ERROR com.abc.crsbatch.controller.Controller - java.lang.NullPointerException
2023-04-18 03:52:25 [main] INFO com.abc.crsbatch.services.TypeService - Clearing data from Temp Table
2023-04-18 03:52:25 [main] INFO com.abc.crsbatch.dao.TypeDao - Cleared Data
2023-04-18 03:52:25 [main] INFO com.abc.crsbatch.controller.Controller - ended

Please help me to get below expected output with last 2 occurrences of Errors using regex expression

2023-04-18 03:52:25 [main] ERROR com.abc.crsbatch.controller.Controller - Exception occurred while Sourcing type Job : TYPE_SOURCING JOB FAILED
2023-04-18 03:52:25 [main] ERROR com.abc.crsbatch.controller.Controller - java.lang.NullPointerException

Hi @Ramesh_Kola1

Try this:
.*ERROR.*

1 Like

@Ramesh_Kola1

system.text.RegularExpressions.Regex.Match(inputstr,“\n[\s\S]+(?<=Exception)”).Value

1 Like

Hi @Ramesh_Kola1

Assign activity --> 
Input= "2023-04-18 05:39:54 [main] INFO com.abc.crsbatch.controller.Controller - Jobs Selected to Run: FUNDING_TYPE_SOURCING Job
2023-04-18 03:52:25 [main] ERROR com.abc.crsbatch.controller.Controller - Exception occurred while Sourcing type Job : TYPE_SOURCING JOB FAILED
2023-04-18 03:52:25 [main] ERROR com.abc.crsbatch.controller.Controller - java.lang.NullPointerException
2023-04-18 03:52:25 [main] INFO com.abc.crsbatch.services.TypeService - Clearing data from Temp Table
2023-04-18 03:52:25 [main] INFO com.abc.crsbatch.dao.TypeDao - Cleared Data
2023-04-18 03:52:25 [main] INFO com.abc.crsbatch.controller.Controller - ended"
Assign activity --> 
Output= System.Text.RegularExpression.Reges.Matches(Input,"(?<=\n\s*)\d+-\d+-\d+.*ERROR[\s\S].*")

DataType of Output: IEnumerable(System.Text.RegularExpression.Match)

Run for For Each loop for Output and print it using Message Box or Log Message

Hope it helps!!

Hi Shiva,

this won’t work in this case error line ends with Exception but if we have any other keywords and line have ERROR keyword we have pull that line.

and pulling all the line except first line but in my case it has to pull only lines with ERROR keyword

@Ramesh_Kola1

system.text.RegularExpressions.Regex.Match(inputstr,“\n[\s\S]+(?<=ERROR).*”).Value

Can you try this once ,It if not works can you please provide your exact output

Hi @Ramesh_Kola1

  1. Read Text File (Output=InputFile

  2. Use Assign Activty

    To:-matches(Type= `System.Text.RegularExpressions.MatchCollection)

    Value:- System.Text.RegularExpressions.Regex.Matches(InputFile, “\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [main] ERROR.*”)

  3. Use Assign Activity

    To:- lastTwoErrors (Type = Array of String)

    Value: - If(matches.Count > 2, matches.Cast(Of System.Text.RegularExpressions.Match).Skip(matches.Count - 2).Select(Function(m) m.Value).ToArray(), matches.Cast(Of System.Text.RegularExpressions.Match).Select(Function(m) m.Value).ToArray())

  4. You can use for each to see all the value lastTwoErrors

Sequence.xaml (10.2 KB)

Hope this helps

Thank you!! it worked

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.