{"id":728,"date":"2022-02-24T14:02:09","date_gmt":"2022-02-24T19:02:09","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=728"},"modified":"2024-02-09T14:44:24","modified_gmt":"2024-02-09T19:44:24","slug":"python-regular-expressions","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/python-regular-expressions\/","title":{"rendered":"Python Regular Expressions"},"content":{"rendered":"\n<p>Python uses re library for regular expressions. re comes with standard python installation. Regular expressions is a very big topic. This page only covers the basics of how to search and substitute. The syntax for patterns is the same as Perl. Consider the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n\nline = \"apple banana carrot date eggplant fig guava\"\n\n# is there a match?\nif re.search(\"carrot\", line):\n    print('match found')\nelse:\n    print('match not found')\n\n# what was matched?\nmat = re.search(r'(.*) date (.*)', line)\nif mat:\n    print(mat.group())\n    print(mat.group(1))\n    print(mat.group(2))\nelse:\n    print('no')\n\n# how to match and substitute a substring\nres = re.sub(r'guava', \"grapefruit\", line)\nif res:\n    print(res)\nelse:\n    print('not substitution')\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>match found\napple banana carrot date eggplant fig guava\napple banana carrot\neggplant fig guava\napple banana carrot date eggplant fig grapefruit<\/code><\/pre>\n\n\n\n<p>First, we need to import re. The two function of re that we are interested in a re are search and sub. Search finds the substring (m\/\/ in Perl). sub finds and substitutes the substring with another (s\/\/\/ in Perl).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python uses re library for regular expressions. re comes with standard python installation. Regular expressions is a very big topic. This page only covers the basics of how to search and substitute. The syntax for patterns is the same as Perl. Consider the following example: output First, we need to import re. The two function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[76,137],"class_list":["post-728","post","type-post","status-publish","format-standard","hentry","category-python","tag-programming","tag-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=728"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/728\/revisions"}],"predecessor-version":[{"id":729,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/728\/revisions\/729"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}