w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
regex - find subsequent lines after a match. Work with pythex but not in script (with python)

You currently have no groups in your pattern, named or otherwise, so the only group you can return is 0, i.e. the entire match.

Address this by either using numeric groups:

title_search = re.compile(r'(?<=Title:)([.
Ww]*)', re.IGNORECASE) 
title = re.search(title_search, data).group(1)

Or named groups:

title_search = re.compile(r'(?<=Title:)(?P<title>[.
Ww]*)', re.IGNORECASE) 
title = re.search(title_search, data).group('title')

Note, you don't need the | symbols in your character set. The pipe symbol is used to indicate a choice between two patterns, but only outside character sets.

Finally, you could use re.DOTALL to simplify your pattern:

title_search =
re.compile(r'(?<=Title:)(?P<title>.*)',re.IGNORECASE | re.DOTALL) 

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.