1

I have a question. I am trying to get a certain string between two substrings from a text file.

The text file is larger, but part of it contains this:

r\n[url=asitename][/url]\r\n[url=x]hello[/url]\r\n[url=x]a text[/url]\r\n[url=n]test[/url]\r\n\ttest
\r\n\t\t\r\n\t\twarn user\r\n\t\r\n\t2\r\n\t02-06-2017, 01:44 am
by cinccino. end: modcp_reports_report -->

Now, I am interested in the text next between 'asitename' and 'end:'

I would like to get the text between those strings.

I have tried this:

lookup = 'asitename'
myFileread = myFile.read()
re.search(lookup+'(.*)end', myFileread).group(1)

However, it returns the following error:

AttributeError: 'NoneType' object has no attribute 'group'

I am confused as to why this is not working. It works with more simple strings.

1
  • 1
    Can you share the output you want? Commented Feb 8, 2017 at 21:37

2 Answers 2

1

Use the re.S flag which will make . match the new line character(s) also:

>>> re.search(r'asitename(.*?)end', myFileread, re.S).group(1)
'][/url]\r\n[url=x]hello[/url]\r\n[url=x]a text[/url]\r\n[url=n]test[/url]\r\n\ttest\n\r\n\t\t\r\n\t\twarn user\r\n\t\r\n\t2\r\n\t02-06-2017, 01:44 am\nby cinccino. 
Sign up to request clarification or add additional context in comments.

Comments

0

Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

It seems .search() is returning None. According to the python api this means there is no position that matches the pattern.

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.