{"id":3155,"date":"2020-10-02T10:10:28","date_gmt":"2020-10-02T04:40:28","guid":{"rendered":"https:\/\/copyassignment.com\/?page_id=3155"},"modified":"2020-12-19T17:52:43","modified_gmt":"2020-12-19T12:22:43","slug":"regular-expression-in-python-regex","status":"publish","type":"page","link":"https:\/\/copyassignment.com\/python\/regular-expression-in-python-regex\/","title":{"rendered":"Regular Expression In Python &#8211; RegEx"},"content":{"rendered":"\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-undefined uagb-toc__collapse uagb-block-b82c81ab\" data-scroll=\"true\" data-offset=\"30\" data-delay=\"585\"><div class=\"uagb-toc__wrap\"><div class=\"uagb-toc__title-wrap\"><div class=\"uagb-toc__title\">Table Of Contents<\/div><span class=\"uag-toc__collapsible-wrap\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewbox=\"0 0 448 512\"><path d=\"M432 416H16a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h416a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16zm0-128H16a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h416a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16zm0-128H16a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h416a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16zm0-128H16A16 16 0 0 0 0 48v32a16 16 0 0 0 16 16h416a16 16 0 0 0 16-16V48a16 16 0 0 0-16-16z\"><\/path><\/svg><\/span><\/div><div class=\"uagb-toc__list-wrap\"><ul class=\"uagb-toc__list\"><li><a href=\"#definition\">Definition<\/a><\/li><li><a href=\"#metacharacters\">Metacharacters<\/a><\/li><li><a href=\"#special-sequences\">Special sequences<\/a><\/li><li><a href=\"#sets\">Sets<\/a><\/li><li><a href=\"#funtions\">Funtions<\/a><\/li><li><a href=\"#findall\">findall()<\/a><\/li><li><a href=\"#search\">search()<\/a><\/li><li><a href=\"#split\">split()<\/a><\/li><li><a href=\"#sub\">sub()<\/a><\/li><\/ul><\/div><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Definition<\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The <strong><span class=\"has-inline-color has-vivid-red-color\"><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/howto\/regex.html\" target=\"_blank\">regex<\/a> <\/span><\/strong>indicates the usage of <strong><span class=\"has-inline-color has-vivid-red-color\">Regular Expression <\/span><\/strong>In Python. The Python regex helps in searching the required pattern by the user i.e. the user can find a pattern or search for a set of strings. To perform regex, the user must first <strong><span class=\"has-inline-color has-vivid-red-color\">import <\/span><\/strong>the <strong><span class=\"has-inline-color has-vivid-red-color\">re <\/span><\/strong>package.<\/p>\n\n\n\n<div class=\"maindiv\"><h2>Syntax:<\/h2><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\n<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>To build a regular expression, the user may use <strong><span class=\"has-inline-color has-vivid-red-color\">metacharacters<\/span><\/strong>, <strong><span class=\"has-inline-color has-vivid-red-color\">special sequences<\/span><\/strong>,<strong><span class=\"has-inline-color has-vivid-red-color\"> <\/span><\/strong>and <strong><span class=\"has-inline-color has-vivid-red-color\">sets<\/span><\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Metacharacters<\/strong><\/h2>\n\n\n\n<p>The <strong><span class=\"has-inline-color has-vivid-red-color\">metacharacters <\/span><\/strong>are special characters used in building the Regular Expression In Python which has <strong><span class=\"has-inline-color has-vivid-red-color\">specific meanings<\/span><\/strong> in it. Some of the commonly used metacharacters are:<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<figure class=\"wp-block-table is-style-stripes, myfontsize is-style-stripes\"><table><thead><tr><th>Metacharacter<\/th><th>Description<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">[]<\/span><\/strong><\/td><td>A condition can be provided to specify the required set of characters.<\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">[a-j], [0-5]<\/span><\/strong><\/td><\/tr><tr><td><span class=\"has-inline-color has-vivid-red-color\"><strong>\\<\/strong>&nbsp;<\/span><\/td><td>This specifies the usage of escape sequence characters like special sequences.<\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">\u201c\\d\u201d<\/span><\/strong><\/td><\/tr><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">.<\/span><\/strong><\/td><td>Specifies any number of characters present between two strings or a set of strings.<\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">py&#8230;n<\/span><\/strong><\/td><\/tr><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">^<\/span><\/strong><\/td><td>This should be placed before the string or any condition to specify that the result must return the string beginning with the given string.<\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">^the<\/span><\/strong><\/td><\/tr><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">$<\/span><\/strong><\/td><td>This should be placed at the end of the string or condition to specify that the result must return the string ending with the given string.<\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">python$<\/span><\/strong><\/td><\/tr><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">*<\/span><\/strong><\/td><td>This should be placed at the end of the given pattern. This specifies that the result must have zero or more occurrences of the given pattern. <\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\"> \u201coo*\u201d<\/span><\/strong><\/td><\/tr><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">+<\/span><\/strong><\/td><td>This should be placed at the end of the given pattern. This specifies that the result must have at least one or more occurrences of the given pattern.<\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\"> \u201coo+\u201d<\/span><\/strong><\/td><\/tr><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">{}<\/span><\/strong><\/td><td> The exact number of required occurrences must be mentioned inside the brackets. <\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">\u201coo{1}\u201d<\/span><\/strong><\/td><\/tr><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">|<\/span><\/strong><\/td><td>Two patterns or string will be given. It will check either one among them is present or not. <\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\"> practice | study<\/span><\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div class=\"maindiv\"><h3>Example1<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Learning python is easy\"\nx = re.findall(\"^Learning.*easy$\", txt)\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">['Learning python is easy']<\/pre><\/strong><\/div><\/div>\n\n\n\n<div class=\"maindiv\"><h3>Example2<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Learning python is easy\"\nx = re.findall(\"py...n\", txt)\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">['python']<\/pre><\/strong><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Special sequences<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The special sequences are like <strong><span class=\"has-inline-color has-vivid-red-color\">escape sequences<\/span><\/strong>. They are followed by a character after <strong><span class=\"has-inline-color has-vivid-red-color\">\\<\/span><\/strong>. Some of the commonly used special sequences are:<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul class=\"myfontsize wp-block-list\"><li><span class=\"has-inline-color has-vivid-red-color\"><strong>\\A<\/strong> <\/span>\u2013 It is placed before the required string to be searched. It returns the matched string<strong>.<\/strong> Example: \\AHello<\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">\\b<\/span><\/strong> \u2013 This is placed before the beginning of the pattern or at the end of the pattern. It returns if the given pattern is found in the beginning or at the end. Example: h\u201d\\bello\u201d \u2013 searches if ello pattern is found at the beginning of any word<strong>.<\/strong> h\u201dello\\b\u201d \u2013 searches if ello pattern is found at the end of any word.<\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">\\B<\/span><\/strong> \u2013 This is the exact opposite of \\b. It returns only the strings when the pattern is not in the beginning or at the end.<\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">\\d<\/span><\/strong> \u2013 This can be used to find whether any numbers are present in the given string.<\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">\\D<\/span><\/strong> \u2013 Exact opposite of \\d. It returns the strings given in the input that does not have any numbers.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"maindiv\"><h3>Example1<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntest_string= \"Hello world\"\nx = re.findall(\"\\AHello\", test_string)\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">['Hello']<\/pre><\/strong><\/div><\/div>\n\n\n\n<div class=\"maindiv\"><h3>Example2<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntest_string= \"Hello world\"\nx = re.findall(r\"ello\\b\", test_string)\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">['ello']<\/pre><\/strong><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Sets<\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The sets are always represented by <strong><span class=\"has-inline-color has-vivid-red-color\">[] <\/span><\/strong>brackets. The sets are <strong><span class=\"has-inline-color has-vivid-red-color\">special characters<\/span><\/strong> placed inside the brackets in Regular Expression In Python. Some of the common usages of sets are:<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul class=\"myfontsize wp-block-list\"><li><span class=\"has-inline-color has-vivid-red-color\"><strong>[xyz]<\/strong> <\/span>\u2013 If any of the characters specified in the sets are matched, then it returns the list of matched characters. <\/li><li><span class=\"has-inline-color has-vivid-red-color\"><strong>[a-j]<\/strong> <\/span>&#8211; If any of the characters specified in the sets are matched, then it returns the list of matched characters in alphabetical order.<\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">[^xyz]<\/span><\/strong> &#8211;&nbsp; Except the characters specified in the sets other characters are returned.<\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">[123] <\/span><\/strong>&#8211; &nbsp;If any of the numbers specified in the sets are matched, then it returns the list of matched numbers.<\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">[0-9]<\/span><\/strong> &#8211; If any of the numbers specified in the sets are matched, then it returns the list of matched numbers in order.<\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">[0-4][0-4]<\/span><\/strong> \u2013 This is used for finding two-digit numbers i.e. it returns the number between 00 and 44.<\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">[*+] <\/span><\/strong>\u2013 The special characters which are specified in the sets are matched, then it returns the list of matched characters.<\/li><\/ul>\n\n\n\n<div class=\"maindiv\"><h3>Example1<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Happy learning...\"\nx = re.findall(\"[hpyz]\", txt)\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">['p', 'p', 'y']<\/pre><\/strong><\/div><\/div>\n\n\n\n<div class=\"maindiv\"><h3>Example2<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Happy learning...\"\nx = re.findall(\"[...]\", txt)\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">['.', '.', '.']<\/pre><\/strong><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Funtions<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The regex expression mainly uses 4 functions. They are:<\/p>\n\n\n\n<ul class=\"myfontsize wp-block-list\"><li><strong><span class=\"has-inline-color has-vivid-red-color\">findall()<\/span><\/strong><\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">search()<\/span><\/strong><\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">split()<\/span><\/strong><\/li><li><strong><span class=\"has-inline-color has-vivid-red-color\">sub()<\/span><\/strong><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>findall()<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The <strong><span class=\"has-inline-color has-vivid-red-color\">findall()<\/span><\/strong> method is used to find all the matches and returns it in the form of a list.<\/p>\n\n\n\n<div class=\"maindiv\"><h3>Example<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Happy learning...\"\nx = re.findall(\"earn\", txt)\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">['earn']<\/pre><\/strong><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong>search<\/strong>()<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The <strong><span class=\"has-inline-color has-vivid-red-color\">search()<\/span><\/strong> method is used to search the given pattern and return the matched items in a list.<\/p>\n\n\n\n<div class=\"maindiv\"><h3>Example1<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Happy learning...\"\nx = re.search(\"earn\", txt)\nif(x):\n    print('Match found')\nelse:\n    print('No match found')\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">Match found<\/pre><\/strong><\/div><\/div>\n\n\n\n<div class=\"maindiv\"><h3>Example2<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Happy learning...\"\nx = re.search(\"python\", txt)\nif(x):\t\n    print('Match found')\nelse:\n    print('No match found')\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">No match found<\/pre><\/strong><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong><strong>split<\/strong><\/strong>()<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The <strong><span class=\"has-inline-color has-vivid-red-color\"><strong>split<\/strong>()<\/span><\/strong> method is used to split the string matched with the condition specified.<\/p>\n\n\n\n<div class=\"maindiv\"><h3>Example<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Happy learning...\"\nx = re.split(\"\\s\", txt) # used to split the text at each white space\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">['Happy', 'learning...']<\/pre><\/strong><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong><strong><strong>sub<\/strong><\/strong><\/strong>()<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The <strong><span class=\"has-inline-color has-vivid-red-color\"><strong>sub<\/strong>()<\/span><\/strong> method is used to substitute the given text at the specified position.<\/p>\n\n\n\n<div class=\"maindiv\"><h3>Example<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Happy python learning...\"\nx = re.sub(\"\\s\",\"-\", txt)\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">Happy-python-learning...<\/pre><\/strong><\/div><\/div>\n\n\n\n<p><strong>Note:<\/strong> For <strong><span class=\"has-inline-color has-vivid-red-color\">sub()<\/span><\/strong>, the user can also pass another parameter mentioning the number of times in which the substitute should be used.<\/p>\n\n\n\n<div class=\"maindiv\"><h3>Example<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import re\ntxt = \"Happy python learning...\"\nx = re.sub(\"\\s\",\"-\", txt,1)\nprint(x)\n<\/pre><\/strong>\n<\/div><h3>Output<\/h3><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">Happy-python learning...<\/pre><\/strong><\/div><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>Also Read:<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/create-language-translator-using-python\/\">Create Language Translator Using Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/get-any-country-date-and-time-using-python\/\">Get Any Country Date And Time Using Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/get-jokes-with-python\/\">Get Jokes with Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/snake-game-in-python-using-pygame\/\">Snake Game in Python using Pygame<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/covid-19-tracker-application-using-python\/\">Covid-19 Tracker Application Using Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/youtube-downloader-application-using-python\/\">YouTube Video Downloader Application Using Python<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Definition The regex indicates the usage of Regular Expression In Python. The Python regex helps in searching the required pattern by the user i.e. the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":833,"menu_order":23,"comment_status":"closed","ping_status":"closed","template":"template-s1-c-page.php","meta":{"footnotes":""},"class_list":["post-3155","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/pages\/3155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=3155"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/pages\/3155\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/pages\/833"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=3155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}