1

I have a string called "strtosearch2" like this:

[02112017 072755 332][1][ERROR]> ----Message : IDC_NO_MEDIA
[02112017 072755 332][1][INFO]> ----              
[02112017 104502 724][1][ERROR]> ----Message : DEV_NOT_READY
[02112017 104502 724][1][INFO]> ----              
[02112017 104503 331][1][ERROR]> ----Message : DEV_NOT_READY
[02112017 104503 331][1][INFO]> ----  

I want to extract the dates which are having the lines "ERROR" only. I wrote my regex as follows:

down2Date= re.findall(r'\[(.*?)\s\d{6}\s\d{3}\]\[\d\]\[ERROR\]',strtosearch2,re.DOTALL)

output as follows:

02112017
02112017 072755 332][1][INFO]> ----              
[02112017
02112017 104502 724][1][INFO]> ----              
[02112017

My target output:

02112017
02112017
02112017

How can I fix this ?. Thank you

2
  • 1
    Remove re.DOTALL. Commented Feb 28, 2018 at 7:23
  • suggestion: sometimes you don't need to define exact input pattern.. for given sample, re.findall(r'(?m)^\[(\d+).*ERROR', strtosearch2) would work too.. if not, try to add relevant sample when asking :) Commented Feb 28, 2018 at 8:14

2 Answers 2

2

You may anchor the pattern at the start of the line/string and remove the re.DOTALL modifier:

re.findall(r'(?m)^\[(.*?)\s\d{6}\s\d{3}]\[\d]\[ERROR]', s)

See the regex demo

With re.DOTALL, the . matched any char including line break chars.

With (?m), ^ matches the start of each line, not only the start of the whole string.

Also, \s can match line break chars, so you might want to use [^\S\r\n] instead of it to only match horizontal whitespace.

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

Comments

0

Try this:

down2Date = re.findall(r'^\[\d+\s\d+\s\d+\]\[\d\]\[ERROR\]', strtosearch2)

3 Comments

can anyone specify what does this re.DOTALL had affected?
@Sanket See my answer.
If the DOTALL flag has been specified, this matches any character including a newline. Details here

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.