Python offers some flags to modify the behavior of regular expression engines. Let’s discuss them below: Show
Case InsensitivityThe re.IGNORECASE allows the regular expression to become case-insensitive. Here, the match is returned based on the case of the provided string, not the string in the regular expression. Python3
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>0 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>1 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>2 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>3 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>4 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>5 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>6 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>7 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>8 Output <_sre.SRE_Match object; span=(0, 13), match='GeeksforGeeks'> Dot Matching NewlineBy 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
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>0 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>1 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>2 None6 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>4 None8 None9 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>7 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>8 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
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>0 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>1 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>2 None6 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>4 None8 <_sre.SRE_Match object; span=(0, 6), match='25-542'>2 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>7 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>8 Output:
Multiline modeWith 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
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>0 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>1 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>2 SUBPATTERN 1 0 0 MAX_REPEAT 2 2 IN CATEGORY CATEGORY_DIGIT LITERAL 45 SUBPATTERN 2 0 0 MAX_REPEAT 3 3 IN CATEGORY CATEGORY_DIGIT <_sre.SRE_Match object; span=(0, 6), match='25-542'>2 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>4 None8 None9 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>7 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>8 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
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>0 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>1 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>2 SUBPATTERN 1 0 0 MAX_REPEAT 2 2 IN CATEGORY CATEGORY_DIGIT LITERAL 45 SUBPATTERN 2 0 0 MAX_REPEAT 3 3 IN CATEGORY CATEGORY_DIGIT <_sre.SRE_Match object; span=(0, 6), match='25-542'>2 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>4 None8 import 8<_sre.SRE_Match object; span=(0, 6), match='Hello,'>7 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>8 Output <_sre.SRE_Match object; span=(7, 12), match='Geeks'> Verbose ModeIt allows representing a regular expression in a more readable way. Let’s look at the below code. Python3
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>0 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>1 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>2 re 8
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>4 6 7<_sre.SRE_Match object; span=(0, 6), match='Hello,'>7 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>8 Output <_sre.SRE_Match object; span=(0, 6), match='25-542'> The Verbose flag treats # character as a comment character and also ignores all the whitespace characters including the line break. Debug ModeThe re.DEBUG flag provides debugging information while compiling a regular expression. Let’s have a look at the below code. Python3
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>0 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>1 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>2 7 8
<_sre.SRE_Match object; span=(0, 6), match='Hello,'>01 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>7 <_sre.SRE_Match object; span=(0, 6), match='Hello,'>8 Output SUBPATTERN 1 0 0 MAX_REPEAT 2 2 IN CATEGORY CATEGORY_DIGIT LITERAL 45 SUBPATTERN 2 0 0 MAX_REPEAT 3 3 IN CATEGORY CATEGORY_DIGIT <_sre.SRE_Match object; span=(0, 6), match='25-542'> Here, you have seen different types of flags that can slightly change the behavior of a regular expression engine. You can also use multiple flags at the same time by using a bitwise OR (|) operator. Modul apa dalam python untuk menjalankan regex?Modul RegEx
Python memiliki paket bawaan yang disebut re , yang dapat digunakan untuk bekerja dengan Ekspresi Reguler.
Apa itu Regex di Python?Regex merupakan singkatan dari Regular Expression yang merupakan serangkaian karakter yang mendefinisikan sebuah pola pencarian. Beberapa bidang yang menggunakan metode ini adalah seperti Natural Language Processing (NLP), Text Mining, Data Validation, Finding Pattern, Anomaly Detection dan lainnya.
Apa itu Regex Pattern?Regex itu adalah sebuah teks dalam bentuk pola untuk pencarian dan banyak dipakai untuk pencocokan, pencarian, dan manipulasi teks.
Apa itu regular expression Javascript?Regular Expression(Regex) adalah pola yang digunakan sebagai kriteria untuk mendapatkan kombinasi karakter pada suatu string.
|