{"id":527,"date":"2018-07-09T07:44:21","date_gmt":"2018-07-09T05:44:21","guid":{"rendered":"http:\/\/pythonprogramming.altervista.org\/?p=527"},"modified":"2018-08-27T07:55:30","modified_gmt":"2018-08-27T05:55:30","slug":"python-regex-or-regular-expressions","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/python-regex-or-regular-expressions\/","title":{"rendered":"Python: regex or regular expressions"},"content":{"rendered":"<h2>Regex: what is it?<\/h2>\n<p><a href=\"http:\/\/pythonprogramming.altervista.org\/python-regex-or-regular-expressions\/regex1\/\" rel=\"attachment wp-att-528\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-528\" src=\"http:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/regex1.jpg\" alt=\"\" width=\"425\" height=\"496\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/regex1.jpg 1076w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/regex1-320x373.jpg 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/regex1-768x896.jpg 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/regex1-960x1120.jpg 960w\" sizes=\"auto, (max-width: 425px) 100vw, 425px\" \/><\/a><\/p>\n<p>A regular expression is a text string used to describe a search pattern.<\/p>\n<pre class=\"lang:default decode:true\" title=\"Find a pattern in a string with regex\"># Regex\r\n# find a particular pattern in a String\r\n\r\ns = 'Waz D: 5 l gu l: 5 GrinVe: 3 P LUK: 2 Cubbi: 1 2 nd dok: 1 maf 74: 1 abr12: 1 Waza D: 5'\r\n\r\nimport re\r\n\r\n# \\w find something starting with a letter or a number\r\n# [\\w\\s]+ followed by any number of letter and space\r\n# : followed by a :\r\n# \\s[0-9] and a space and a number\r\n\r\nx = re.findall(r\"\\w[\\w\\s]+:\\s[0-9]\", s)\r\nprint(*x, sep=\"\\n\")<\/pre>\n<p>In the previous code the goal was to find a name followed by &#8216;:&#8217; a space and a number and to put them in a list. We use the &#8216;re&#8217; module of Python. After we import it, we define the string &#8216;s&#8217; where we have to find the piece of the string that behave like the pattern. The pattern is made as follow:<\/p>\n<pre>\r\n\r\n\\w ................ a letter or a number\r\n\r\n[\\w\\s]+ .......... as many letters or numbers following the letter\/number\r\n\r\n: ................... a column\r\n\r\n\\s[0-9] ........... a space and then a number\r\n<\/pre>\n<h2>Video explanation<\/h2>\n<p><iframe loading=\"lazy\" title=\"Python and regex: example of findall\" width=\"747\" height=\"560\" src=\"https:\/\/www.youtube.com\/embed\/6-t_DxJN7D0?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n\t<script>\r\nvar title = \"Regex\";\r\nvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-regex-or-regular-expressions\/\",\"Regex - findall\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/using-the-re-module-to-grab-numbers-in-a-text-and-make-different-tests-with-few-code\/\",\"Practical example with regex\"]\r\n\t\t];\t\t \r\n<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\n","protected":false},"excerpt":{"rendered":"How to find a pattern in a string with regex\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/python-regex-or-regular-expressions\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":537,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[148,50],"tags":[151,153,150,4,152,149],"class_list":["post-527","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-regex","category-video","tag-findall","tag-import-re","tag-pattern","tag-python","tag-re-findall","tag-regex"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=527"}],"version-history":[{"count":6,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"predecessor-version":[{"id":549,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/527\/revisions\/549"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/537"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}