Loading...
<div class="whitepaper-body-page">
<p>Also called as regex. They allow us to search and match specific patterns of text<strong>.</strong> Raw string – It is nothing, but a regular string prefixed with r, this tells python not to handle any backslashes ‘\’ ,’\n’,’\t’ in any special way</p>
<p>To use the regular expression, we first need to import re.</p>
<p>match() method checks for the given pattern at the start of the string if it finds a match it will return it as match object along with the span().Here span() method defines the start and end index value of the matches object.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">x = </span><span class="enlighter-s0">'this is my first regular expression'</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">pattern = re.</span><span class="enlighter-m1">match</span><span class="enlighter-g1">(</span><span class="enlighter-text">r</span><span class="enlighter-s0">'this'</span><span class="enlighter-text">,x</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">pattern</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">x = 'this is my first regular expression'
pattern = re.match(r'this',x)
print(pattern)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">x = 'this is my first regular expression'
pattern = re.match(r'this',x)
print(pattern)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-1720" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-1720" aria-expanded="false">Show Output</button><div id="ac-1720" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code><_sre.SRE_Match object; span=(0, 4), match='this'></code></pre>
</div></div>
<p>Search() method: Used to match pattern anywhere in between the string.If a match is found it return the matched object along with its span.Search() will stop at the first match itself so if we have multiple object matching the pattern then that cannot be done using search() method </p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> re</span></div></div><div class=""><div><span class="enlighter-text">x = </span><span class="enlighter-s0">'this is my first regular expression'</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">pattern = re.</span><span class="enlighter-m1">search</span><span class="enlighter-g1">(</span><span class="enlighter-text">r</span><span class="enlighter-s0">'first'</span><span class="enlighter-text">,x</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">pattern</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">import re
x = 'this is my first regular expression'
pattern = re.search(r'first',x)
print(pattern)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re
x = 'this is my first regular expression'
pattern = re.search(r'first',x)
print(pattern)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-63612" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-63612" aria-expanded="false">Show Output</button><div id="ac-63612" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code><_sre.SRE_Match object; span=(11, 16), match='first'></code></pre>
</div></div>
<p>Findall() method It will keep on checking the match till no data is available for matching. And returns the result in form of list.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> re</span></div></div><div class=""><div><span class="enlighter-text">x = </span><span class="enlighter-s0">'this is my first regular expression'</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">pattern = re.</span><span class="enlighter-m1">findall</span><span class="enlighter-g1">(</span><span class="enlighter-text">r</span><span class="enlighter-s0">'e'</span><span class="enlighter-text">,x</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">pattern</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">import re
x = 'this is my first regular expression'
pattern = re.findall(r'e',x)
print(pattern)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re
x = 'this is my first regular expression'
pattern = re.findall(r'e',x)
print(pattern)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-35553" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-35553" aria-expanded="false">Show Output</button><div id="ac-35553" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>['e', 'e', 'e']</code></pre>
</div></div>
<p>compile() method compiles a regular expression pattern into a regular expression object, which can be used for matching using its match() and search() methods, described below. This is useful when you have to use the same object several times in your program.</p>
<p>finditer() method matches the string and returns the value as iterator in the order found. The <em>string</em> is scanned left-to-right, and matches are returned in the order found. The iterator object is exhausted when u call it once.See below example.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> re</span></div></div><div class=""><div><span class="enlighter-text">x = </span><span class="enlighter-s0">'this is my first regular expression'</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">pattern=re.</span><span class="enlighter-m1">compile</span><span class="enlighter-g1">(</span><span class="enlighter-s0">'e'</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">p=pattern.</span><span class="enlighter-m1">finditer</span><span class="enlighter-g1">(</span><span class="enlighter-text">x</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k1">for</span><span class="enlighter-text"> i </span><span class="enlighter-k0">in</span><span class="enlighter-text"> p:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">i</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">import re
x = 'this is my first regular expression'
pattern=re.compile('e')
p=pattern.finditer(x)
for i in p:
print(i)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re
x = 'this is my first regular expression'
pattern=re.compile('e')
p=pattern.finditer(x)
for i in p:
print(i)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-98059" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-98059" aria-expanded="false">Show Output</button><div id="ac-98059" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code><_sre.SRE_Match object; span=(18, 19), match='e'>
<_sre.SRE_Match object; span=(25, 26), match='e'>
<_sre.SRE_Match object; span=(29, 30), match='e'></code></pre>
</div></div>
<h4 class="wp-block-heading">Special Characters in Regular expression</h4>
<p>\d = matches only numbers (0-9)</p>
<p>\D = matches everything except a number</p>
<p>\w = matches word character (a-z, A-Z, 0-9), small letter, capital letter and numbers but does not match special characters and escape sequences</p>
<p>\W = matches not a word character</p>
<p>\s = matches white space (\n, \t)</p>
<p>\S = matches not whitespace</p>
<p>\b = matches boundary</p>
<p>\B = matched not a boundry</p>
<p>$ = matches end of the string</p>
<p>^ = matches start of the string</p>
<p>[] = matches characters or digits in brackets its called character class or character set<p></p></p>
<p>[^] = matches character not in brackets, it return value which is negative of whatever is present in the brackets<p></p></p>
<p>Example : Program to match everything except ‘rat’ from the given string</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> re</span></div></div><div class=""><div><span class="enlighter-text">y=</span><span class="enlighter-s0">'cat mat pat rat bat'</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">pattern = re.</span><span class="enlighter-m1">compile</span><span class="enlighter-g1">(</span><span class="enlighter-text">r'</span><span class="enlighter-g1">[</span><span class="enlighter-text">^r</span><span class="enlighter-g1">]</span><span class="enlighter-text">at’</span></div></div><div class=""><div><span class="enlighter-text">matches = pattern.</span><span class="enlighter-m1">finditer</span><span class="enlighter-g1">(</span><span class="enlighter-text">y</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k1">for</span><span class="enlighter-text"> match </span><span class="enlighter-k0">in</span><span class="enlighter-text"> matches:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">match</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">import re
y='cat mat pat rat bat'
pattern = re.compile(r'[^r]at’
matches = pattern.finditer(y)
for match in matches:
print(match)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re
y='cat mat pat rat bat'
pattern = re.compile(r'[^r]at’
matches = pattern.finditer(y)
for match in matches:
print(match)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-62935" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-62935" aria-expanded="false">Show Output</button><div id="ac-62935" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code><_sre.SRE_Match object; span=(0, 3), match='cat'>
<_sre.SRE_Match object; span=(4, 7), match='mat'>
<_sre.SRE_Match object; span=(8, 11), match='pat'>
<_sre.SRE_Match object; span=(16, 19), match='bat'></code></pre>
</div></div>
<p>Example: Program to match all ip address in a given string</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">text_search=</span><span class="enlighter-s5">'''</span></div></div><div class=""><div><span class="enlighter-s5">abcdefghijklmnopqrstuvwxyz</span></div></div><div class=""><div><span class="enlighter-s5">ABCDEFGHIJKLMNOPQRSTUVWXYZ</span></div></div><div class=""><div><span class="enlighter-s5">123456789</span></div></div><div class=""><div><span class="enlighter-s5">Hi HiHi</span></div></div><div class=""><div><span class="enlighter-s5">Metacharacters needed to be escaped:</span></div></div><div class=""><div><span class="enlighter-s5">.^<em>$+?[]|()</span></div></div><div class=""><div><span class="enlighter-s5">rstforum.net</span></div></div><div class=""><div><span class="enlighter-s5">123-456-7891</span></div></div><div class=""><div><span class="enlighter-s5">192.168.120.12</span></div></div><div class=""><div><span class="enlighter-s5">192.168.120.13</span></div></div><div class=""><div><span class="enlighter-s5">1.1.1.1</span></div></div><div class=""><div><span class="enlighter-s5">22.22.22.22</span></div></div><div class=""><div><span class="enlighter-s5">100.100.100.100</span></div></div><div class=""><div><span class="enlighter-s5">Mr. Gupta</span></div></div><div class=""><div><span class="enlighter-s5">Mr Kaushik</span></div></div><div class=""><div><span class="enlighter-s5">Ms.Swati</span></div></div><div class=""><div><span class="enlighter-s5">Mrs Bose</span></div></div><div class=""><div><span class="enlighter-s5">Mr.T</span></div></div><div class=""><div><span class="enlighter-s5">'''</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k0">import</span><span class="enlighter-text"> re</span></div></div><div class=""><div><span class="enlighter-text">pattern = re.</span><span class="enlighter-m1">compile</span><span class="enlighter-g1">(</span><span class="enlighter-text">r</span><span class="enlighter-s0">'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}'</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text">matches = pattern.</span><span class="enlighter-m1">finditer</span><span class="enlighter-g1">(</span><span class="enlighter-text">text_search</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k1">for</span><span class="enlighter-text"> match </span><span class="enlighter-k0">in</span><span class="enlighter-text"> matches:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">match</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">text_search='''
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
123456789
Hi HiHi
Metacharacters needed to be escaped:
.^</em>$+?[]|()
rstforum.net
123-456-7891
192.168.120.12
192.168.120.13
1.1.1.1
22.22.22.22
100.100.100.100
Mr. Gupta
Mr Kaushik
Ms.Swati
Mrs Bose
Mr.T
'''
import re
pattern = re.compile(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}')<br>matches = pattern.finditer(text_search)
for match in matches:
print(match)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text_search='''
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
123456789
Hi HiHi
Metacharacters needed to be escaped:
.^*$+?[]|()
rstforum.net
123-456-7891
192.168.120.12
192.168.120.13
1.1.1.1
22.22.22.22
100.100.100.100
Mr. Gupta
Mr Kaushik
Ms.Swati
Mrs Bose
Mr.T
'''
import re
pattern = re.compile(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}')<br>matches = pattern.finditer(text_search)
for match in matches:
print(match)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-39135" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-39135" aria-expanded="false">Show Output</button><div id="ac-39135" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code><_sre.SRE_Match object; span=(149, 163), match='192.168.120.12'>
<_sre.SRE_Match object; span=(164, 178), match='192.168.120.13'>
<_sre.SRE_Match object; span=(179, 186), match='1.1.1.1'>
<_sre.SRE_Match object; span=(187, 198), match='22.22.22.22'>
<_sre.SRE_Match object; span=(199, 214), match='100.100.100.100'></code></pre>
</div></div>
<h4 class="wp-block-heading">Quantifiers in regular expression</h4>
<p>In all above example we were matching one digit or one character at a time.We use quantifier if we have to match multiple digits.</p>
<ul><li>* : 0 or more occurrence of special characters</li><li>+ : 1 or more occurrence of special characters</li><li>? : 0 or one occurrence of special characters</li><li>{3} : exact number, these many digits</li><li>{3,4} : range of number (minimum, maximum)</li></ul>
<p>Example : Program to match names starting with Mr or Mrs</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">text_search=</span><span class="enlighter-s5">'''</span></div></div><div class=""><div><span class="enlighter-s5">abcdefghijklmnopqrstuvwxyz</span></div></div><div class=""><div><span class="enlighter-s5">ABCDEFGHIJKLMNOPQRSTUVWXYZ</span></div></div><div class=""><div><span class="enlighter-s5">123456789</span></div></div><div class=""><div><span class="enlighter-s5">Hi HiHi</span></div></div><div class=""><div><span class="enlighter-s5">Metacharacters needed to be escaped:</span></div></div><div class=""><div><span class="enlighter-s5">.^<em>$+?[]|()</span></div></div><div class=""><div><span class="enlighter-s5">rstforum.net</span></div></div><div class=""><div><span class="enlighter-s5">123-456-7891</span></div></div><div class=""><div><span class="enlighter-s5">192.168.120.12</span></div></div><div class=""><div><span class="enlighter-s5">192.168.120.13</span></div></div><div class=""><div><span class="enlighter-s5">1.1.1.1</span></div></div><div class=""><div><span class="enlighter-s5">22.22.22.22</span></div></div><div class=""><div><span class="enlighter-s5">100.100.100.100</span></div></div><div class=""><div><span class="enlighter-s5">Mr. Gupta</span></div></div><div class=""><div><span class="enlighter-s5">Mr Kaushik</span></div></div><div class=""><div><span class="enlighter-s5">Mrs Bose</span></div></div><div class=""><div><span class="enlighter-s5">Mr.T</span></div></div><div class=""><div><span class="enlighter-s5">'''</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k0">import</span><span class="enlighter-text"> re</span></div></div><div class=""><div><span class="enlighter-text">pattern = re.</span><span class="enlighter-m1">compile</span><span class="enlighter-g1">(</span><span class="enlighter-text">r</span><span class="enlighter-s0">'Mrs</em>[. a-zA-Z]<em>'</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text">matches = pattern.</span><span class="enlighter-m1">finditer</span><span class="enlighter-g1">(</span><span class="enlighter-text">text_search</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k1">for</span><span class="enlighter-text"> match </span><span class="enlighter-k0">in</span><span class="enlighter-text"> matches:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">match</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div></div><div class="enlighter-raw">text_search='''
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
123456789
Hi HiHi
Metacharacters needed to be escaped:
.^</em>$+?[]|()
rstforum.net
123-456-7891
192.168.120.12
192.168.120.13
1.1.1.1
22.22.22.22
100.100.100.100
Mr. Gupta
Mr Kaushik
Mrs Bose
Mr.T
'''
import re
pattern = re.compile(r'Mrs<em>[. a-zA-Z]</em>')<br>matches = pattern.finditer(text_search)
for match in matches:
print(match) </div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text_search='''
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
123456789
Hi HiHi
Metacharacters needed to be escaped:
.^<em>$+?[]|()
rstforum.net
123-456-7891
192.168.120.12
192.168.120.13
1.1.1.1
22.22.22.22
100.100.100.100
Mr. Gupta
Mr Kaushik
Mrs Bose
Mr.T
'''
import re
pattern = re.compile(r'Mrs</em>[. a-zA-Z]*')<br>matches = pattern.finditer(text_search)
for match in matches:
print(match) </pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-94012" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-94012" aria-expanded="false">Show Output</button><div id="ac-94012" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code><_sre.SRE_Match object; span=(215, 224), match='Mr. Gupta'>
<_sre.SRE_Match object; span=(225, 235), match='Mr Kaushik'>
<_sre.SRE_Match object; span=(245, 253), match='Mrs Bose'>
<_sre.SRE_Match object; span=(254, 258), match='Mr.T'></code></pre>
</div></div>
<h4 class="wp-block-heading"><strong>Groups</strong> in regular expression</h4>
<p>A better way of doing this is using groups which allow you to match specific groups. Frequently you need to obtain more information than just whether the RE matched or not. Regular expressions are often used to dissect strings by writing a RE divided into several subgroups which match different components of interest.</p>
<p>It becomes much easier to solve the above example using groups</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">text_search=</span><span class="enlighter-s5">'''</span></div></div><div class=""><div><span class="enlighter-s5">abcdefghijklmnopqrstuvwxyz</span></div></div><div class=""><div><span class="enlighter-s5">ABCDEFGHIJKLMNOPQRSTUVWXYZ</span></div></div><div class=""><div><span class="enlighter-s5">123456789</span></div></div><div class=""><div><span class="enlighter-s5">Hi HiHi</span></div></div><div class=""><div><span class="enlighter-s5">123-456-7891</span></div></div><div class=""><div><span class="enlighter-s5">192.168.120.12</span></div></div><div class=""><div><span class="enlighter-s5">192.168.120.13</span></div></div><div class=""><div><span class="enlighter-s5">1.1.1.1</span></div></div><div class=""><div><span class="enlighter-s5">22.22.22.22</span></div></div><div class=""><div><span class="enlighter-s5">100.100.100.100</span></div></div><div class=""><div><span class="enlighter-s5">Mr. Gupta</span></div></div><div class=""><div><span class="enlighter-s5">Mr Kaushik</span></div></div><div class=""><div><span class="enlighter-s5">Mrs Bose</span></div></div><div class=""><div><span class="enlighter-s5">Mr.T</span></div></div><div class=""><div><span class="enlighter-s5">Ms.Swati</span></div></div><div class=""><div><span class="enlighter-s5">SameerShah@gmail.com</span></div></div><div class=""><div><span class="enlighter-s5">Swati.Kore@education.net</span></div></div><div class=""><div><span class="enlighter-s5">Mahes-98-patil@abc-ty.co.in</span></div></div><div class=""><div><span class="enlighter-s5">'''</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k0">import</span><span class="enlighter-text"> re</span></div></div><div class=""><div><span class="enlighter-text">pattern = re.</span><span class="enlighter-m1">compile</span><span class="enlighter-g1">(</span><span class="enlighter-text">r</span><span class="enlighter-s0">'(Mr|Ms|Mrs).?\s?[A-Z]\w<em>'</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text">matches = pattern.</span><span class="enlighter-m1">finditer</span><span class="enlighter-g1">(</span><span class="enlighter-text">text_search</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k1">for</span><span class="enlighter-text"> match </span><span class="enlighter-k0">in</span><span class="enlighter-text"> matches:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">match</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div></div><div class="enlighter-raw">text_search='''
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
123456789
Hi HiHi
123-456-7891
192.168.120.12
192.168.120.13
1.1.1.1
22.22.22.22
100.100.100.100
Mr. Gupta
Mr Kaushik
Mrs Bose
Mr.T
Ms.Swati
SameerShah@gmail.com
Swati.Kore@education.net
Mahes-98-patil@abc-ty.co.in
'''
import re
pattern = re.compile(r'(Mr|Ms|Mrs).?\s?[A-Z]\w</em>')<br>matches = pattern.finditer(text_search)
for match in matches:
print(match) </div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text_search='''
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
123456789
Hi HiHi
123-456-7891
192.168.120.12
192.168.120.13
1.1.1.1
22.22.22.22
100.100.100.100
Mr. Gupta
Mr Kaushik
Mrs Bose
Mr.T
Ms.Swati
SameerShah@gmail.com
Swati.Kore@education.net
Mahes-98-patil@abc-ty.co.in
'''
import re
pattern = re.compile(r'(Mr|Ms|Mrs).?\s?[A-Z]\w*')<br>matches = pattern.finditer(text_search)
for match in matches:
print(match) </pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-81322" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-81322" aria-expanded="false">Show Output</button><div id="ac-81322" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code><_sre.SRE_Match object; span=(152, 161), match='Mr. Gupta'>
<_sre.SRE_Match object; span=(162, 172), match='Mr Kaushik'>
<_sre.SRE_Match object; span=(173, 181), match='Mrs Bose'>
<_sre.SRE_Match object; span=(182, 186), match='Mr.T'>
<_sre.SRE_Match object; span=(187, 195), match='Ms.Swati'></code></pre>
</div></div>
<p>Example : program to search all email id’s from given string</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-text">text_search=</span><span class="enlighter-s5">'''</span></div></div><div class=""><div><span class="enlighter-s5">abcdefghijklmnopqrstuvwxyz</span></div></div><div class=""><div><span class="enlighter-s5">ABCDEFGHIJKLMNOPQRSTUVWXYZ</span></div></div><div class=""><div><span class="enlighter-s5">123456789</span></div></div><div class=""><div><span class="enlighter-s5">Hi HiHi</span></div></div><div class=""><div><span class="enlighter-s5">123-456-7891</span></div></div><div class=""><div><span class="enlighter-s5">192.168.120.12</span></div></div><div class=""><div><span class="enlighter-s5">192.168.120.13</span></div></div><div class=""><div><span class="enlighter-s5">1.1.1.1</span></div></div><div class=""><div><span class="enlighter-s5">22.22.22.22</span></div></div><div class=""><div><span class="enlighter-s5">100.100.100.100</span></div></div><div class=""><div><span class="enlighter-s5">Mr. Gupta</span></div></div><div class=""><div><span class="enlighter-s5">Mr Kaushik</span></div></div><div class=""><div><span class="enlighter-s5">Mrs Bose</span></div></div><div class=""><div><span class="enlighter-s5">Mr.T</span></div></div><div class=""><div><span class="enlighter-s5">SameerShah@gmail.com</span></div></div><div class=""><div><span class="enlighter-s5">Swati.Kore@education.net</span></div></div><div class=""><div><span class="enlighter-s5">Mahes-98-patil@abc-ty.co.in</span></div></div><div class=""><div><span class="enlighter-s5">'''</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k0">import</span><span class="enlighter-text"> re</span></div></div><div class=""><div><span class="enlighter-text">pattern = re.</span><span class="enlighter-m1">compile</span><span class="enlighter-g1">(</span><span class="enlighter-text">r</span><span class="enlighter-s0">'[a-zA-Z0-9.-]+@[a-zA-Z-]+.(com|net|co|in)'</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text">matches = pattern.</span><span class="enlighter-m1">finditer</span><span class="enlighter-g1">(</span><span class="enlighter-text">text_search</span><span class="enlighter-g1">)</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-k1">for</span><span class="enlighter-text"> match </span><span class="enlighter-k0">in</span><span class="enlighter-text"> matches:</span></div></div><div class=""><div><span class="enlighter-text"> </span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">match</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div></div><div class="enlighter-raw">text_search='''
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
123456789
Hi HiHi
123-456-7891
192.168.120.12
192.168.120.13
1.1.1.1
22.22.22.22
100.100.100.100
Mr. Gupta
Mr Kaushik
Mrs Bose
Mr.T
SameerShah@gmail.com
Swati.Kore@education.net
Mahes-98-patil@abc-ty.co.in
'''
import re
pattern = re.compile(r'[a-zA-Z0-9.-]+@[a-zA-Z-]+.(com|net|co|in)')<br>matches = pattern.finditer(text_search)
for match in matches:
print(match) </div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text_search='''
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
123456789
Hi HiHi
123-456-7891
192.168.120.12
192.168.120.13
1.1.1.1
22.22.22.22
100.100.100.100
Mr. Gupta
Mr Kaushik
Mrs Bose
Mr.T
SameerShah@gmail.com
Swati.Kore@education.net
Mahes-98-patil@abc-ty.co.in
'''
import re
pattern = re.compile(r'[a-zA-Z0-9.-]+@[a-zA-Z-]+.(com|net|co|in)')<br>matches = pattern.finditer(text_search)
for match in matches:
print(match) </pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-5528" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-5528" aria-expanded="false">Show Output</button><div id="ac-5528" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code><_sre.SRE_Match object; span=(187, 207), match='SameerShah@gmail.com'>
<_sre.SRE_Match object; span=(208, 232), match='Swati.Kore@education.net'>
<_sre.SRE_Match object; span=(233, 257), match='Mahes-98-patil@abc-ty.co'></code></pre>
</div></div>
<h4 class="wp-block-heading"><strong>Search and Replace</strong> in regular expression</h4>
<p>Another common task is to find all the matches for a pattern, and replace them with a different string. The sub()method takes a replacement value, which can be either a string or a function, and the string to be processed.</p>
<div class="enlighter-default enlighter-v-standard enlighter-t-minimal enlighter-hover enlighter-linenumbers "><div class="enlighter-toolbar-top enlighter-toolbar"><div class="enlighter-btn enlighter-btn-raw" title="Plain text"></div><div class="enlighter-btn enlighter-btn-copy" title="Copy to clipboard"></div><div class="enlighter-btn enlighter-btn-window" title="Open code in new window"></div><div class="enlighter-btn enlighter-btn-website" title="EnlighterJS 3 Syntax Highlighter"></div></div><div class="enlighter" style=""><div class=""><div><span class="enlighter-k0">import</span><span class="enlighter-text"> re</span></div></div><div class=""><div><span class="enlighter-text">x=</span><span class="enlighter-s0">'Simple text with numbers 12345'</span><span class="enlighter-text"></span></div></div><div class=""><div><span class="enlighter-text">pattern = re.</span><span class="enlighter-m1">sub</span><span class="enlighter-g1">(</span><span class="enlighter-text">r</span><span class="enlighter-s0">'\d'</span><span class="enlighter-text">,</span><span class="enlighter-s0">'6'</span><span class="enlighter-text">,x</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span></div></div><div class=""><div><span class="enlighter-text"></span><span class="enlighter-m0">print</span><span class="enlighter-g1">(</span><span class="enlighter-text">pattern</span><span class="enlighter-g1">)</span></div></div></div><div class="enlighter-raw">import re
x='Simple text with numbers 12345'
pattern = re.sub(r'\d','6',x)<br>print(pattern)</div><div class="enlighter-toolbar-bottom enlighter-toolbar"></div></div><pre class="EnlighterJSRAW enlighter-origin" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re
x='Simple text with numbers 12345'
pattern = re.sub(r'\d','6',x)<br>print(pattern)</pre>
<div class="wp-block-pb-accordion-item c-accordion__item js-accordion-item" data-initially-open="false" data-click-to-close="true" data-auto-close="true" data-scroll="false" data-scroll-offset="0"><button id="at-89235" class="c-accordion__title js-accordion-controller c-accordion__title--button" role="button" tabindex="0" aria-controls="ac-89235" aria-expanded="false">Show Output</button><div id="ac-89235" class="c-accordion__content" hidden="hidden">
<pre class="wp-block-code"><code>Simple text with numbers 66666</code></pre>
</div></div>
<p></div></p>