<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pro-Tek Blog &#187; Python Regular Expressions</title>
	<atom:link href="http://www.pro-tekconsulting.com/blog/tag/python-regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pro-tekconsulting.com/blog</link>
	<description>For UI developers / UI designers and UI trends</description>
	<lastBuildDate>Thu, 05 Sep 2019 03:59:47 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.34</generator>
	<item>
		<title>REGULAR EXPRESSIONS IN PYTHON</title>
		<link>http://www.pro-tekconsulting.com/blog/regular-expressions-in-python/</link>
		<comments>http://www.pro-tekconsulting.com/blog/regular-expressions-in-python/#comments</comments>
		<pubDate>Wed, 08 Nov 2017 04:37:34 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[Python Regular Expressions]]></category>

		<guid isPermaLink="false">http://www.pro-tekconsulting.com/blog/?p=2262</guid>
		<description><![CDATA[<p>Regular Expressions in Python A regular expression is a distinctive sequence of characters that helps the user to match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The module re offers full support for Perl-like regular expressions in Python. [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://www.pro-tekconsulting.com/blog/regular-expressions-in-python/">REGULAR EXPRESSIONS IN PYTHON</a> appeared first on <a rel="nofollow" href="http://www.pro-tekconsulting.com/blog">Pro-Tek Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h4>Regular Expressions in Python</h4>
<p>A regular expression is a distinctive sequence of characters that helps the user to match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The module re offers full support for Perl-like regular expressions in Python.</p>
<p>In this article we will be covering important functions, which would be used to handle regular expressions.</p>
<p><strong>IN PYTHON A REGULAR EXPRESSION SEARCH IS TYPICALLY WRITTEN AS</strong></p>
<p>match = re.search(pat, str)</p>
<p>The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None. Therefore, the search is usually followed by an if-statement to test if the search is succeeded or not.</p>
<p>str = &#8216;an example word:cat!!&#8217;<br />
match = re.search(r&#8217;word:\w\w\w&#8217;, str)<br />
# If-statement after search() tests if it succeeded<br />
if match:<br />
print &#8216;found&#8217;, match.group() ## &#8216;found word:cat&#8217;<br />
else:<br />
print &#8216;did not find&#8217;</p>
<p>The code match = re.search(pat, str) stores the search result in a variable named &#8220;match&#8221;. Then the if-statement tests the match &#8212; if true the search succeeded and match.group() is the matching text (e.g. &#8216;word:cat&#8217;). Otherwise if the match is false, then the search will not be succeeded, and there is no matching text.</p>
<p>The &#8216;r&#8217; at the start of the pattern string designates a python &#8220;raw&#8221; string which passes through backslashes without change which is very convenient for regular expressions.</p>
<p>if-statement tests the match &#8212; if true the search succeeded and match.group() is the matching text (e.g. &#8216;word:cat&#8217;). If the match is false, then the search will not be succeeded, and there is no matching text.</p>
<p>The &#8216;r&#8217; at the start of the pattern string designates a python &#8220;raw&#8221; string which passes through backslashes without change which is very convenient for regular expressions.</p>
<p><strong>THE MATCH FUNCTION</strong></p>
<p>The function attempts to match re pattern to string with optional flags.</p>
<p>Here is the syntax for this function:<br />
re.match(pattern, string, flags=0)</p>
<p>The re.match function returns a match object on success. The usegroup(num) or groups() function is used to match objects to get matched expression.</p>
<p><img class="aligncenter wp-image-2263 size-full" src="http://www.pro-tekconsulting.com/blog/wp-content/uploads/2017/11/The-match-function.png" alt="The match function" width="534" height="127" /></p>
<p>#!/usr/bin/python<br />
import re<br />
line = &#8220;Cats are smarter than dogs&#8221;<br />
matchObj = re.match( r'(.*) are (.*?) .*&#8217;, line, re.M|re.I)<br />
if matchObj:<br />
print &#8220;matchObj.group() : &#8220;, matchObj. group ()<br />
print &#8220;matchObj.group(1) : &#8220;, matchObj. group (1)<br />
print &#8220;matchObj.group(2) : &#8220;, matchObj. group (2)<br />
else:<br />
print &#8220;No match!!&#8221;</p>
<p>When the above code is executed, it gives the following result:</p>
<p>matchObj.group() : Cats are smarter than dogs<br />
matchObj.group(1) : Cats<br />
matchObj.group(2) : smarter</p>
<p>The post <a rel="nofollow" href="http://www.pro-tekconsulting.com/blog/regular-expressions-in-python/">REGULAR EXPRESSIONS IN PYTHON</a> appeared first on <a rel="nofollow" href="http://www.pro-tekconsulting.com/blog">Pro-Tek Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pro-tekconsulting.com/blog/regular-expressions-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
