Dot Matching Newline
By using re.DOTALL flag, you can modify the behavior of dot (.) character to match the newline character apart from other characters. Before using the DOTALL flag, let’s look into how regular engine responds to the newline character.
Python3
import re match = re.search(r '.+' , 'Hello,\nGeeks' ) print (match) |
Output
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>
Here, the regular expression matches one or more characters (‘. +’). At the time when the engine reaches the newline character, it stops, because the dot character doesn’t match the line breaks. Let’s look into the code that makes use of the DOTALL flag.
Python3
import re match = re.search(r '.+' , 'Hello,\nGeeks' , re.DOTALL) print (match) |
Output:
<_sre.SRE_Match object; span=(0, 12), match=’Hello,\nGeeks’>
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