1

I am trying to create a Regex to match patterns existing in multiple lines. The code is as follows:

(.*Action name.*\n.*Action status.*)[\n+]*(good)

The text string is as follows:

Workflow name:icl_prd_db_logs
Action name:backup
Action status:failed
hi
how r u
good
perfect
Code successfully completed
Another code failed

Now I can use one \n if I want to match Action name and Action status because those are sequential lines. But, if I want to match a line which exists after several lines then a single \n will not work. In other words, how can I use \n multiple times something like [\n]+ or [\n]*+ or [\n+](.*).

I don't want to use any arrays just I need a single line code which can map all the existing pattern matches. Does anyone know how to do that?

In the above-mentioned code, I am trying to search good pattern which is two lines away from the Action status line so I have tried to use [\n+]* but it doesn't work at all. Can anyone help me to sort out this code?

2
  • 1
    There are a lot of questions on SO that presume that regular expressions can do more than they can. When someone wants a magic catch-all regular expression that can be very complicated and even more difficult to maintain. Whereas a solution that involves several lines of code or a few regular expressions can be more easily understood and more easily maintained. Is there a reason why "I need a single line code"? Commented Feb 1, 2018 at 8:59
  • Of course looking for multiple consecutive line breaks doesn’t work, because you don’t have any of those here. Those lines all contain characters before the line break. Commented Feb 1, 2018 at 9:00

1 Answer 1

2

You can write (.*\n.*)* for multipal line regex. So finally your regex is (.*Action name.*(\n)+.*Action status.*)(.*\n.*)*(good).

https://regex101.com/r/yGpz5v/1

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick support Pathik. I have succesfully tested this pattern match condition in my environment. I am facing one more issue for large amounts of data in log file and due to that there is an error called 'Catastrophic backtracking' issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.