Multiline mode
With the Multiline flag, you can match against the beginning and the end of any line within the string. If we look into the ^ character, it will only match against the beginning of a string. So, even if there is a matching character after the newline character, It returns none. Let’s look into the below code.
Python3
import re match = re.search(r '^Geeks' , 'Hello,\nGeeks' ) print (match) |
Output
None
Using the Multiline flag, you can overcome the above issue. It can match against the beginning and end of any line in the string. Let’s match against the beginning of a string.
Python3
import re match = re.search(r '^Geeks' , 'Hello,\nGeeks' , re.MULTILINE) print (match) |
Output
<_sre.SRE_Match object; span=(7, 12), match='Geeks'>
Python Flags to Tune the Behavior of Regular Expressions
Python offers some flags to modify the behavior of regular expression engines. Let’s discuss them below:
- Case Insensitivity
- Dot Matching Newline
- Multiline Mode
- Verbose Mode
- Debug Mode