{"id":1067,"date":"2017-08-19T06:59:36","date_gmt":"2017-08-19T06:59:36","guid":{"rendered":"http:\/\/punjabirevolution.com\/?page_id=1067"},"modified":"2017-08-25T17:19:57","modified_gmt":"2017-08-25T17:19:57","slug":"regular-expression","status":"publish","type":"page","link":"http:\/\/punjabirevolution.com\/index.php\/regular-expression\/","title":{"rendered":"Regular Expression"},"content":{"rendered":"<h1>Regular Expression<\/h1>\n<p>regular expression is a smart wart to formulate the rule of a language.<\/p>\n<ol>\n<li>natural languages<\/li>\n<li>formal or regular languages.<\/li>\n<\/ol>\n<p>The natural languages are human languages. We speak them without caring about the grammar. The grammar is formulated after analyzing the language.<\/p>\n<p>The regular languages are systematically formulated according to rules that we form in the form of regular expressions.<\/p>\n<p>So, you can every regular expression is&nbsp; basically a rule to formulate a language. For any language we need alphabets. These alphabets are coined together&nbsp; to form the language. For instance say a(b|c) is a regular expression where a,b,c are alphabets. The regular expression suggests that the language for this regulare expression would be<\/p>\n<p>L = {ab, ac}.<\/p>\n<p>For natural languages like English we have twenty six alphabets AB&hellip;Z and have countless rules to coin them together to make countless ways of expressions. The regular languages have hard and fast rules. The natural languages also have some rules but the rules may be violated in some exceptional cases.<\/p>\n<h2>Using Regular Expression for Language Processing<\/h2>\n<p>We can use regular expressions for processing the text. They can be used to search and substitute by applying the complex conditions.<\/p>\n<h3>Simple string look-up<\/h3>\n<p>We use forward slashes for simple string look up. For example<\/p>\n<p>\/is\/ is a regular expression that would find the string &ldquo;is&rdquo;. The string &ldquo;is&rdquo; may be an individual word like in string &ldquo;He is going to university&rdquo; or part of a word like &ldquo;I am a Pak<span style=\"text-decoration: underline;\">is<\/span>tani&rdquo;.<\/p>\n<h3><strong>Complete word look-up<\/strong><\/h3>\n<p>To search a string as a standalone word, we&nbsp; can use the notation \\b that defines word boundary. \/is\\b\/ will make sure that the string ends at word&rsquo;s end. so It will not bring out &ldquo;Pakistan&rdquo; or &ldquo;istambol&rdquo; but it may bring the words &ldquo;<span class=\"st\" data-hveid=\"65\" data-ved=\"0ahUKEwjWl_fp7OLVAhVB2xoKHfxJAewQ4EUIQTAD\">chemoprophylaxis<\/span>&ldquo;, &ldquo;<span class=\"st\" data-hveid=\"65\" data-ved=\"0ahUKEwjWl_fp7OLVAhVB2xoKHfxJAewQ4EUIQTAD\">megasporogenesis<\/span>&ldquo;. So we need to apply bound in start of the pattern as well. So the right regular expression pattern to find out a complete word is<\/p>\n<p>\/\\bis\\b\/<\/p>\n<h3>Selecting one from a range of characters<\/h3>\n<p>Lets say you are searching the frequency of word &ldquo;he&rdquo; in a text. but you know at the start of the sentence it would be written with capital &ldquo;H&rdquo; as &ldquo;He&rdquo; and otherwise as &ldquo;he&rdquo;.&nbsp; Keeping in mind both forms we may write following regular expression.<\/p>\n<p>\/\\b[Hh]e\\b\/<\/p>\n<p>So, in short, the notation [] to select one character from a range. Say, you are trying to list down all the vehicle numbers with digits 2222. Suppose every vehicle number has three alphabets followed by four digits. so the regular expression for his scenario would be<\/p>\n<p>\/[ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ]2222\/<\/p>\n<p>Listing each every character within [] is quite cumbersome. So there is a short way for it. We can write it as \/[A-Z][A-Z][A-Z]2222.<\/p>\n<p>&nbsp;<\/p>\n<h3>Not Selecting one from a range of characters<\/h3>\n<p>With the selection from range notation [], we can use a caret sign ^ as not character. For instance \/[A-Z]123\/ means any character from English alphabets followed by 123 and \/[^A-Z]123\/ means any non-alphabetic character followed by 123.<\/p>\n<h3>{} in regular expression<\/h3>\n<p>For the following regular expression we have&nbsp;unnecessary repetition.<\/p>\n<p>\/[A-Z][A-Z][A-Z]2222.<\/p>\n<p>There is also a workaround for it and that is.<\/p>\n<p>\/[A-Z]{3}2222}<\/p>\n<p>Once again the repetition of 2 can also removed as \/[A-Z]{3}2{4}\/<\/p>\n<p>We can also specify the range of repetition as<\/p>\n<p>\/ab{3}\/ means abbb<br>\n\/ab{2,5}\/ means the repetition may be 2 times. 3 times, 4 times and 5 times. ie. abb, abbb, abbbb and abbbb are all legal words for this regular expression.<br>\n\/ab{3,}\/ means the repetition is at minimum three times. so three or more b&rsquo;s are all allowed. so abbb,abbbb,abbbbb,abbbbb,abbbbbbbbb&hellip;&hellip; are all legal words.<\/p>\n<h3>Use of () with {}<\/h3>\n<p>The symbol {} is used for repetition of single character. The regular expressin \/ab{5}\/ means repetition of b five times. What if we want to repeat the pair ab five times as ababababab? In this case, we may use the parenthesis () as \/(ab){5}\/.<\/p>\n<h3>period . in regular expression.<\/h3>\n<p>The period . in regular expression means any character. so for RE \/c.t\/ any character can be filled in between c and t. cat, cbt, c t, c7t, c&amp;t all are legal. Of course ct is illegal. It means null is not allowed.<\/p>\n<h3>Question mark ? in regular expression.<\/h3>\n<p>The question mark ? gives you an optional case. For instance, while searching for the word &ldquo;colour&rdquo; you may come across two versions of it. The American write it as color whereas in British English it is spelled as &ldquo;colour&rdquo; To search out both versions from text we need to make the character &ldquo;u&rdquo; as optional. The question mark ? comes to rescue in this situation.<\/p>\n<p>\/colou?r\/<\/p>\n<p>By default the question mark applies to the adjacent character only. We may add () to apply it to multiple characters.<\/p>\n<h2>Kleene star * and Kleene plus + operators<\/h2>\n<p>The * operator means zero to many and + means at least one.<\/p>\n<p>\/ba*\/<\/p>\n<p>it means b, ba, baa, baaa, baaaaaa, baaaaaaaaaaaaaa&hellip;&hellip;.. all are invalid.<\/p>\n<p>\/ba+\/<\/p>\n<p>it means, we need at least one a followed by b.&nbsp; so ba, baa, baaa, baaa&hellip;&hellip;. valid but &ldquo;b&rdquo; is invalid.<\/p>\n<h2>caret ^ and dollar sign $ in regular expression<\/h2>\n<p>We already saw that caret ^ is used as not operator inside [] symbols. but it has different meaning in start of the sentence.<\/p>\n<p>\/^Pakistan\/<\/p>\n<p>Here the word Pakistan would be searched only if at&nbsp; the start of given search.<\/p>\n<p>\/Pakistan$\/<\/p>\n<p>Here the word Pakistan would be found only if it is the last word in the text. In other words ^ means that matching would start from the beginning and $ means the matching would start from the end of the text.<\/p>\n<p>\/^Pakistan$\/<\/p>\n<p>It means the matching would start both from beginning and the end of the text. It would bring the result if &ldquo;Pakistan&rdquo; is the only word in text.<\/p>\n<h2>Backslashed Characters<\/h2>\n<p>As we saw, * , +&nbsp; and . have special meaning in regular expression. What if we want to write them. As in all other programming languages we can use backslash in this situation. So . means any character and \\. mean actual dot. Here is table of backslahed character.<\/p>\n<figure id=\"attachment_1076\" aria-describedby=\"caption-attachment-1076\" style=\"width: 594px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/punjabirevolution.com\/wp-content\/uploads\/2017\/08\/baclslashed-characters.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-1076\" src=\"http:\/\/punjabirevolution.com\/wp-content\/uploads\/2017\/08\/baclslashed-characters.jpg\" alt=\"baclslashed-characters\" width=\"594\" height=\"158\" srcset=\"http:\/\/punjabirevolution.com\/wp-content\/uploads\/2017\/08\/baclslashed-characters.jpg 594w, http:\/\/punjabirevolution.com\/wp-content\/uploads\/2017\/08\/baclslashed-characters-300x80.jpg 300w\" sizes=\"(max-width: 594px) 85vw, 594px\"\/><\/a><figcaption id=\"caption-attachment-1076\" class=\"wp-caption-text\">baclslashed-characters<\/figcaption><\/figure>\n<h2>Shorts for common set of characters<\/h2>\n<p>The most common set of characters can be represented by in short way. For example the [0-9] can be written as \\d. Here is the complete set.<\/p>\n<figure id=\"attachment_1077\" aria-describedby=\"caption-attachment-1077\" style=\"width: 606px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/punjabirevolution.com\/wp-content\/uploads\/2017\/08\/Short-way-for-common-set-of-characters.jpg\"><img loading=\"lazy\" class=\"size-full wp-image-1077\" src=\"http:\/\/punjabirevolution.com\/wp-content\/uploads\/2017\/08\/Short-way-for-common-set-of-characters.jpg\" alt=\"Short way for common set of characters\" width=\"606\" height=\"190\" srcset=\"http:\/\/punjabirevolution.com\/wp-content\/uploads\/2017\/08\/Short-way-for-common-set-of-characters.jpg 606w, http:\/\/punjabirevolution.com\/wp-content\/uploads\/2017\/08\/Short-way-for-common-set-of-characters-300x94.jpg 300w\" sizes=\"(max-width: 606px) 85vw, 606px\"\/><\/a><figcaption id=\"caption-attachment-1077\" class=\"wp-caption-text\">Short way for common set of characters<\/figcaption><\/figure>\n<h2>() and \\ for referring back in regular expression.<\/h2>\n<p>We can use () and \\ together to refer to a memory.<\/p>\n<p>\/I am a (man)&nbsp; of (words) and he is also&nbsp; a \\1&nbsp; of \\2 \/<\/p>\n<p>Here \\1 refers to man and \\2 refers to words. This regular expression would search for the following string in text<\/p>\n<p>&ldquo;I am a man&nbsp; of words and he is also&nbsp; a man of words&rdquo;<\/p>\n<p>if I write it as<\/p>\n<p>\/I am a (man)&nbsp; of (words) and he is also&nbsp; a \\2&nbsp; of \\1 \/<\/p>\n<p>Then the following string would be searched&nbsp; in the text.<\/p>\n<p>&ldquo;I am a man of words and words is also a man. &rdquo;<\/p>\n<h2>Substitution in regular expression<\/h2>\n<p>s\/colour\/color\/<\/p>\n<p>This regular expression will substitute the word &ldquo;colour&rdquo; with &ldquo;color&rdquo; wherever found in text.<\/p>\n<p>s\/([0-9]+)\/&lt;\\1&gt;\/<\/p>\n<p>The above regular expression would enclose the numbers (wherever found in the text)&nbsp; with angle brackets.<\/p>\n<p>s\/(.*) is (.*) \/ \\1 are \\2 \/<\/p>\n<p>Here the word &ldquo;is&rdquo; will be replaced with &ldquo;are&rdquo; everything else would remain the same. \\1 refers to anything before is and \\2 refers to anything after is.<\/p>\n<h2>Substitution Syntax in Python.<\/h2>\n<pre class=\"lang:python decode:true\">import re\r\n\r\ninput = \"They is good boys\"\r\ninput = re.sub(r'(.*)is(.*)','\\g&lt;1&gt;are\\g&lt;2&gt;',input)\r\n\r\nprint(input)<\/pre>\n<p>Line 1: we imported library for regular expression.<\/p>\n<p>Line 3: a sample input<\/p>\n<p>Line 4: the re.sub(searched pattern, replaced pattern, tex) is the library function to search and replace a string from the given text. The () brackets are used for putting in memory and \\g&lt;1&gt; are used to refer to that memory. \\g&lt;1&gt; to first memory and \\g&lt;2&gt; to second memory.<\/p>\n<p>Line 6: prints the output that is &ldquo;They are good boys.&rdquo;<\/p>\n<p>That&rsquo;s it . Enjoy!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Regular Expression regular expression is a smart wart to formulate the rule of a language. natural languages formal or regular languages. The natural languages are human languages. We speak them without caring about the grammar. The grammar is formulated after analyzing the language. The regular languages are systematically formulated according to rules that we form &hellip; <a href=\"http:\/\/punjabirevolution.com\/index.php\/regular-expression\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Regular Expression&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/pages\/1067"}],"collection":[{"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/comments?post=1067"}],"version-history":[{"count":13,"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/pages\/1067\/revisions"}],"predecessor-version":[{"id":1116,"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/pages\/1067\/revisions\/1116"}],"wp:attachment":[{"href":"http:\/\/punjabirevolution.com\/index.php\/wp-json\/wp\/v2\/media?parent=1067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}