0

I need to separate lines of strings and so far i got to the point where i need to get the end of them. And that's where my problem is.

My regex looks like this:

//these work fine
$firstRegex = "/^\w{3}\s+\d\s\d{2}:\d{2}:\d{2}/i";
$secondRegex = "/\w{1,}\s\w{4}\[\d{4}\]/i";
$thirdRegex = "/\w{1,}\(.*?\)/";
//this is the incorrect regex
$fourthRegex = "/(\w+\s+)+\(.*\)$/";

Here are some of the lines that i need this regex for:

Oct 6 15:39:01 linux_server CRON[5921]: pam_unix(cron:session): session opened for user root by (uid=0)

Oct 6 15:39:01 linux_server CRON[5921]: pam_unix(cron:session): session closed for user root

They should be with either brackets or without them. My result is:

Oct 6 15:39:01 linux_server CRON[5921] pam_unix(cron:session)

Oct 6 15:39:01 linux_server CRON[5921] pam_unix(cron:session)

1
  • What is the issue? Is it that the last regex must match whitespace separated words followed with an optional parenthesized part? Then try \w+(?:\s+\w+)*(?:\s*\(.*\))?$. See this regex demo. Commented Oct 18, 2019 at 19:53

2 Answers 2

1

For this you will have to use positive look behind, I am trying to match all the characters that follow ):

(?<=\)\:).*

You can use regexr.com to work with regex.

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

Comments

0

Try Regex: ^(\w{3} \d{1,2}\s\d{2}:\d{2}:\d{2}) (\w+) (\w{4}\[\d{4}\]): (\w+?\(.*?\)): ([\w ]+)(\(.*?\))?$

Demo

Comments

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.