{"id":193,"date":"2025-08-28T14:54:30","date_gmt":"2025-08-28T14:54:30","guid":{"rendered":"https:\/\/learn.rantissi.my.id\/?p=193"},"modified":"2025-08-28T14:54:31","modified_gmt":"2025-08-28T14:54:31","slug":"regular-expression-regex-di-python","status":"publish","type":"post","link":"https:\/\/learn.rantissi.my.id\/index.php\/2025\/08\/28\/regular-expression-regex-di-python\/","title":{"rendered":"Regular Expression (Regex) di Python"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Apa itu Regex?<\/h2>\n\n\n\n<p>Regex = <strong>pola pencarian teks<\/strong>.<\/p>\n\n\n\n<p>Contoh:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"^[A-Z]\"<\/code> \u2192 cari teks yang <strong>dimulai dengan huruf besar<\/strong><\/li>\n\n\n\n<li><code>\"\\d{4}\"<\/code> \u2192 cari <strong>empat digit angka<\/strong><\/li>\n\n\n\n<li><code>\"[a-zA-Z]+\"<\/code> \u2192 cari satu atau lebih huruf (tanpa angka\/simbol)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Import Modul <code>re<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Fungsi-fungsi Dasar:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <code>re.match()<\/code><\/h3>\n\n\n\n<p>Mencocokkan pola <strong>di awal string<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>re.match(r\"Ran\", \"Ran belajar Python\")  # \u2705 cocok\nre.match(r\"belajar\", \"Ran belajar\")     # \u274c tidak cocok\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <code>re.search()<\/code><\/h3>\n\n\n\n<p>Mencari <strong>di mana saja dalam string<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>re.search(r\"belajar\", \"Ran sedang belajar Python\")  # \u2705\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <code>re.findall()<\/code><\/h3>\n\n\n\n<p>Mengambil semua <strong>kemunculan yang cocok<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>re.findall(r\"\\d+\", \"Kode: 123 dan 456\")  # &#91;'123', '456']\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Contoh Validasi Email<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>email = \"ran@example.com\"\npattern = r\"^&#91;\\w\\.-]+@&#91;\\w\\.-]+\\.\\w+$\"\n\nif re.match(pattern, email):\n    print(\"Email valid!\")\nelse:\n    print(\"Email tidak valid!\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Contoh Validasi Nomor HP Indonesia<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>hp = \"08123456789\"\npattern = r\"^08\\d{8,10}$\"\n\nif re.match(pattern, hp):\n    print(\"Nomor valid!\")\nelse:\n    print(\"Nomor tidak valid!\")<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apa itu Regex? Regex = pola pencarian teks. Contoh: Import Modul re Fungsi-fungsi Dasar: 1. re.match() Mencocokkan pola di awal string 2. re.search() Mencari di mana saja dalam string 3. re.findall() Mengambil semua kemunculan yang cocok Contoh Validasi Email Contoh Validasi Nomor HP Indonesia<\/p>\n","protected":false},"author":1,"featured_media":20,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[],"class_list":["post-193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-python"],"_links":{"self":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/comments?post=193"}],"version-history":[{"count":2,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts\/193\/revisions\/195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/media\/20"}],"wp:attachment":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}