{"id":1479,"date":"2025-09-27T21:54:31","date_gmt":"2025-09-27T14:54:31","guid":{"rendered":"https:\/\/kienthucmo.com\/?p=1479"},"modified":"2025-11-05T23:33:34","modified_gmt":"2025-11-05T16:33:34","slug":"tim-kiem-va-thay-the-chuoi-trong-python","status":"publish","type":"post","link":"https:\/\/kienthucmo.com\/vi\/tim-kiem-va-thay-the-chuoi-trong-python\/","title":{"rendered":"T\u00ecm ki\u1ebfm v\u00e0 thay th\u1ebf chu\u1ed7i trong Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Trong l\u1eadp tr\u00ecnh, chu\u1ed7i (string) lu\u00f4n l\u00e0 m\u1ed9t trong nh\u1eefng ki\u1ec3u d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng nhi\u1ec1u nh\u1ea5t. Khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u v\u0103n b\u1ea3n, nh\u1eadt k\u00fd h\u1ec7 th\u1ed1ng, hay th\u1eadm ch\u00ed l\u00e0 d\u1eef li\u1ec7u t\u1eeb ng\u01b0\u1eddi d\u00f9ng, vi\u1ec7c <strong>t\u00ecm ki\u1ebfm v\u00e0 thay th\u1ebf chu\u1ed7i<\/strong> tr\u1edf th\u00e0nh thao t\u00e1c kh\u00f4ng th\u1ec3 thi\u1ebfu. M\u00ecnh th\u01b0\u1eddng g\u1eb7p t\u00ecnh hu\u1ed1ng ph\u1ea3i t\u00ecm ki\u1ebfm m\u1ed9t t\u1eeb kh\u00f3a trong v\u0103n b\u1ea3n r\u1ed3i thay \u0111\u1ed5i n\u00f3, ho\u1eb7c l\u00e0m s\u1ea1ch d\u1eef li\u1ec7u b\u1eb1ng c\u00e1ch lo\u1ea1i b\u1ecf k\u00fd t\u1ef1 \u0111\u1eb7c bi\u1ec7t.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Trong Python, c\u00f3 r\u1ea5t nhi\u1ec1u c\u00e1ch \u0111\u1ec3 th\u1ef1c hi\u1ec7n t\u00ecm ki\u1ebfm v\u00e0 thay th\u1ebf chu\u1ed7i \u2013 t\u1eeb c\u00e1c ph\u01b0\u01a1ng th\u1ee9c chu\u1ed7i c\u01a1 b\u1ea3n cho \u0111\u1ebfn c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd h\u01a1n l\u00e0 <strong>Regular Expression (regex)<\/strong>. B\u00e0i vi\u1ebft n\u00e0y m\u00ecnh s\u1ebd t\u1ed5ng h\u1ee3p l\u1ea1i c\u00e1c ph\u01b0\u01a1ng ph\u00e1p t\u1eeb \u0111\u01a1n gi\u1ea3n \u0111\u1ebfn n\u00e2ng cao, k\u00e8m v\u00ed d\u1ee5 minh h\u1ecda, \u0111\u1ec3 gi\u00fap vi\u1ec7c x\u1eed l\u00fd chu\u1ed7i tr\u1edf n\u00ean hi\u1ec7u qu\u1ea3 h\u01a1n.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. C\u00e1c ph\u01b0\u01a1ng ph\u00e1p t\u00ecm ki\u1ebfm chu\u1ed7i<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 S\u1eed d\u1ee5ng to\u00e1n t\u1eed <code>in<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u0110\u00e2y l\u00e0 c\u00e1ch \u0111\u01a1n gi\u1ea3n v\u00e0 d\u1ec5 hi\u1ec3u nh\u1ea5t \u0111\u1ec3 ki\u1ec3m tra xem m\u1ed9t chu\u1ed7i con c\u00f3 t\u1ed3n t\u1ea1i trong chu\u1ed7i g\u1ed1c hay kh\u00f4ng.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>text = \"Python is a powerful programming language.\"\n\n# Check if \"Python\" exists in text\nif \"Python\" in text:\n    print(\"Found 'Python' in text\")\nelse:\n    print(\"Not found\")\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Python is a powerful programming language.<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Check if &quot;Python&quot; exists in text<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Python<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Found &#39;Python&#39; in text<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">else<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Not found<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">K\u1ebft qu\u1ea3: <code>Found 'Python' in text<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To\u00e1n t\u1eed <code><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\"><strong>in<\/strong><\/mark><\/code> r\u1ea5t h\u1eefu \u00edch khi m\u00ecnh ch\u1ec9 c\u1ea7n ki\u1ec3m tra nhanh s\u1ef1 t\u1ed3n t\u1ea1i m\u00e0 kh\u00f4ng quan t\u00e2m \u0111\u1ebfn v\u1ecb tr\u00ed xu\u1ea5t hi\u1ec7n.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 S\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <code>find()<\/code> v\u00e0 <code>index()<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C\u1ea3 hai ph\u01b0\u01a1ng th\u1ee9c n\u00e0y \u0111\u1ec1u tr\u1ea3 v\u1ec1 <strong>ch\u1ec9 s\u1ed1 (index)<\/strong> c\u1ee7a chu\u1ed7i con trong chu\u1ed7i g\u1ed1c. \u0110i\u1ec3m kh\u00e1c bi\u1ec7t l\u00e0:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>find()<\/strong><\/code> tr\u1ea3 v\u1ec1 <code>-1<\/code> n\u1ebfu kh\u00f4ng t\u00ecm th\u1ea5y.<\/li>\n\n\n\n<li><code><strong>index()<\/strong><\/code> n\u00e9m l\u1ed7i <code>ValueError<\/code> n\u1ebfu kh\u00f4ng t\u00ecm th\u1ea5y.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>text = \"I love coding in Python.\"\n\n# Using find()\npos = text.find(\"Python\")  # returns 16\nprint(\"Position of 'Python':\", pos)\n\n# Using index()\ntry:\n    pos2 = text.index(\"Java\")\nexcept ValueError:\n    print(\"'Java' not found in text\")\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">I love coding in Python.<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Using find()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">pos <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">find<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Python<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># returns 16<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Position of &#39;Python&#39;:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> pos<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Using index()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">try<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    pos2 <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">index<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Java<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">except<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">ValueError<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">&#39;Java&#39; not found in text<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">M\u00ecnh th\u01b0\u1eddng d\u00f9ng <mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\"><code>find()<\/code> <\/mark>khi mu\u1ed1n ki\u1ec3m tra nh\u1eb9 nh\u00e0ng, c\u00f2n <code><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">index()<\/mark><\/code> ph\u00f9 h\u1ee3p khi c\u1ea7n ch\u1eafc ch\u1eafn chu\u1ed7i con t\u1ed3n t\u1ea1i.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 S\u1eed d\u1ee5ng <code>startswith()<\/code> v\u00e0 <code>endswith()<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Khi c\u1ea7n ki\u1ec3m tra chu\u1ed7i c\u00f3 b\u1eaft \u0111\u1ea7u ho\u1eb7c k\u1ebft th\u00fac b\u1eb1ng m\u1ed9t m\u1eabu n\u00e0o \u0111\u00f3, hai ph\u01b0\u01a1ng th\u1ee9c n\u00e0y c\u1ef1c k\u1ef3 ti\u1ec7n l\u1ee3i.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>filename = \"report_2025.pdf\"\n\nprint(filename.startswith(\"report\"))   # True\nprint(filename.endswith(\".pdf\"))       # True\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">filename <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">report_2025.pdf<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">filename<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">startswith<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">report<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #D8DEE9FF\">   <\/span><span style=\"color: #616E88\"># True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">filename<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">endswith<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">.pdf<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #D8DEE9FF\">       <\/span><span style=\"color: #616E88\"># True<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Nh\u1eefng h\u00e0m n\u00e0y \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch khi x\u1eed l\u00fd t\u00ean file, URL, hay chu\u1ed7i c\u1ea5u tr\u00fac chu\u1ea9n.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.4. S\u1eed d\u1ee5ng Regular Expression (regex)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Regex mang l\u1ea1i kh\u1ea3 n\u0103ng t\u00ecm ki\u1ebfm linh ho\u1ea1t v\u00e0 m\u1ea1nh m\u1ebd h\u01a1n. Python cung c\u1ea5p th\u01b0 vi\u1ec7n <code>re<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import re\n\ntext = \"My email is contact@example.com\"\n\n# Search for email pattern\nmatch = re.search(r\"\\b&#91;\\w.-&#93;+@&#91;\\w.-&#93;+\\.\\w+\\b\", text)\nif match:\n    print(\"Email found:\", match.group())\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">My email is contact@example.com<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Search for email pattern<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">match <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">search<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #EBCB8B\">\\b<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #EBCB8B\">\\w.-<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #EBCB8B\">@<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #EBCB8B\">\\w.-<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #EBCB8B\">\\.\\w<\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #EBCB8B\">\\b<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> match<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Email found:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> match<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">group<\/span><span style=\"color: #ECEFF4\">())<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Regex c\u00f3 th\u1ec3 ph\u00e1t hi\u1ec7n m\u1eabu ph\u1ee9c t\u1ea1p nh\u01b0 email, s\u1ed1 \u0111i\u1ec7n tho\u1ea1i, ng\u00e0y th\u00e1ng m\u00e0 ph\u01b0\u01a1ng th\u1ee9c chu\u1ed7i b\u00ecnh th\u01b0\u1eddng kh\u00f3 l\u00e0m \u0111\u01b0\u1ee3c.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. C\u00e1c ph\u01b0\u01a1ng ph\u00e1p thay th\u1ebf chu\u1ed7i<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1. S\u1eed d\u1ee5ng <code>replace()<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u0110\u00e2y l\u00e0 c\u00e1ch c\u01a1 b\u1ea3n nh\u1ea5t, gi\u00fap thay th\u1ebf t\u1ea5t c\u1ea3 ho\u1eb7c m\u1ed9t s\u1ed1 l\u1ea7n xu\u1ea5t hi\u1ec7n c\u1ee7a chu\u1ed7i con.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>text = \"Python is great. I love Python.\"\n\n# Replace all\nnew_text = text.replace(\"Python\", \"Java\")\nprint(new_text)  # \"Java is great. I love Java\"\n\n# Replace only first occurrence\nnew_text2 = text.replace(\"Python\", \"Java\", 1)\nprint(new_text2)  # \"Java is great. I love Python\"\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Python is great. I love Python.<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Replace all<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">new_text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">replace<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Python<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Java<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">new_text<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &quot;Java is great. I love Java&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Replace only first occurrence<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">new_text2 <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">replace<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Python<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Java<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">new_text2<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &quot;Java is great. I love Python&quot;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>replace()<\/code> c\u1ef1c k\u1ef3 d\u1ec5 d\u00f9ng, nh\u01b0ng ch\u1ec9 l\u00e0m vi\u1ec7c v\u1edbi chu\u1ed7i c\u1ee5 th\u1ec3, kh\u00f4ng \u00e1p d\u1ee5ng \u0111\u01b0\u1ee3c cho m\u1eabu ph\u1ee9c t\u1ea1p.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.2. S\u1eed d\u1ee5ng Regular Expression \u2013 <code>re.sub()<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Khi c\u1ea7n thay th\u1ebf d\u1ef1a tr\u00ean m\u1eabu regex, m\u00ecnh d\u00f9ng <code>re.sub()<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import re\n\ntext = \"The price is 100 dollars.\"\n\n# Replace digits with 'XXX'\nnew_text = re.sub(r\"\\d+\", \"XXX\", text)\nprint(new_text)  # \"The price is XXX dollars.\"\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">The price is 100 dollars.<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Replace digits with &#39;<\/span><span style=\"color: #81A1C1\">XXX<\/span><span style=\"color: #616E88\">&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">new_text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sub<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #EBCB8B\">\\d<\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">XXX<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">new_text<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &quot;The price is <\/span><span style=\"color: #81A1C1\">XXX<\/span><span style=\"color: #616E88\"> dollars.&quot;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Ngo\u00e0i ra, c\u00f3 th\u1ec3 t\u1eadn d\u1ee5ng <strong>capturing groups<\/strong> \u0111\u1ec3 thay th\u1ebf th\u00f4ng minh h\u01a1n:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import re\n\ntext = \"Contact: 123-456-7890\"\n\n# Replace keeping last 4 digits only\nnew_text = re.sub(r\"\\d{3}-\\d{3}-(\\d{4})\", r\"***-***-\\1\", text)\nprint(new_text)  # \"Contact: ***-***-7890\"<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Contact: 123-456-7890<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Replace keeping last 4 digits only<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">new_text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sub<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #EBCB8B\">\\d<\/span><span style=\"color: #81A1C1\">{3}<\/span><span style=\"color: #EBCB8B\">-\\d<\/span><span style=\"color: #81A1C1\">{3}<\/span><span style=\"color: #EBCB8B\">-<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #EBCB8B\">\\d<\/span><span style=\"color: #81A1C1\">{4}<\/span><span style=\"color: #ECEFF4\">)&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #81A1C1\">***<\/span><span style=\"color: #EBCB8B\">-<\/span><span style=\"color: #81A1C1\">***<\/span><span style=\"color: #EBCB8B\">-<\/span><span style=\"color: #81A1C1\">\\1<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">new_text<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &quot;Contact: ***-***-7890&quot;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. C\u00e1c t\u00ecnh hu\u1ed1ng n\u00e2ng cao<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 Thay th\u1ebf kh\u00f4ng ph\u00e2n bi\u1ec7t hoa \u2013 th\u01b0\u1eddng<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">M\u1eb7c \u0111\u1ecbnh, <code><strong>replace()<\/strong><\/code> ph\u00e2n bi\u1ec7t hoa th\u01b0\u1eddng. V\u1edbi <strong>regex<\/strong>, m\u00ecnh c\u00f3 th\u1ec3 d\u00f9ng <strong><code><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">re.IGNORECASE<\/mark><\/code>.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import re\n\ntext = \"Python is fun. PYTHON is powerful.\"\n\nnew_text = re.sub(r\"python\", \"Java\", text, flags=re.IGNORECASE)\nprint(new_text)  # \"Java is fun. Java is powerful.\"<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Python is fun. PYTHON is powerful.<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">new_text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sub<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #EBCB8B\">python<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Java<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">flags<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\">re<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9FF\">IGNORECASE<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">new_text<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &quot;Java is fun. Java is powerful.&quot;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 Thay th\u1ebf c\u00f3 \u0111i\u1ec1u ki\u1ec7n b\u1eb1ng callback function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>re.sub()<\/strong><\/code> cho ph\u00e9p truy\u1ec1n m\u1ed9t h\u00e0m \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh c\u00e1ch thay th\u1ebf. \u0110i\u1ec1u n\u00e0y c\u1ef1c h\u1eefu \u00edch khi mu\u1ed1n thay \u0111\u1ed5i linh ho\u1ea1t d\u1ef1a tr\u00ean n\u1ed9i dung t\u00ecm \u0111\u01b0\u1ee3c.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import re\n\ntext = \"Order: item1=20, item2=50, item3=100\"\n\ndef add_prefix(match):\n    number = int(match.group())\n    return f\"${number}\"\n\nnew_text = re.sub(r\"\\d+\", add_prefix, text)\nprint(new_text)  # \"Order: item1=$20, item2=$50, item3=$100\"<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Order: item1=20, item2=50, item3=100<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">add_prefix<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">match<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    number <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">int<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">match<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">group<\/span><span style=\"color: #ECEFF4\">())<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">f<\/span><span style=\"color: #A3BE8C\">&quot;$<\/span><span style=\"color: #EBCB8B\">{<\/span><span style=\"color: #D8DEE9FF\">number<\/span><span style=\"color: #EBCB8B\">}<\/span><span style=\"color: #A3BE8C\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">new_text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sub<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #EBCB8B\">\\d<\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> add_prefix<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">new_text<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &quot;Order: item1=$20, item2=$50, item3=$100&quot;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 L\u00e0m s\u1ea1ch d\u1eef li\u1ec7u v\u0103n b\u1ea3n<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">M\u1ed9t \u1ee9ng d\u1ee5ng \u0111i\u1ec3n h\u00ecnh l\u00e0 l\u00e0m s\u1ea1ch v\u0103n b\u1ea3n: lo\u1ea1i b\u1ecf k\u00fd t\u1ef1 \u0111\u1eb7c bi\u1ec7t, chu\u1ea9n h\u00f3a kho\u1ea3ng tr\u1eafng.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import re\n\ntext = \"Hello!!!   Welcome   to Python...   \"\n\n# Remove special characters\nclean_text = re.sub(r\"&#91;^a-zA-Z0-9\\s&#93;\", \"\", text)\n\n# Normalize spaces\nclean_text = re.sub(r\"\\s+\", \" \", clean_text).strip()\n\nprint(clean_text)  # \"Hello Welcome to Python\"\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Hello!!!   Welcome   to Python...   <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Remove special characters<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">clean_text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sub<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">r<\/span><span style=\"color: #ECEFF4\">&quot;&#91;<\/span><span style=\"color: #81A1C1\">^<\/span><span style=\"color: #EBCB8B\">a-zA-Z0-9\\s<\/span><span style=\"color: #ECEFF4\">&#93;&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> text<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Normalize spaces<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">clean_text <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> re<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sub<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">r<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #EBCB8B\">\\s<\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> clean_text<\/span><span style=\"color: #ECEFF4\">).<\/span><span style=\"color: #88C0D0\">strip<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">clean_text<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &quot;Hello Welcome to Python&quot;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">M\u00ecnh th\u01b0\u1eddng \u00e1p d\u1ee5ng k\u1ef9 thu\u1eadt n\u00e0y khi x\u1eed l\u00fd d\u1eef li\u1ec7u t\u1eeb form nh\u1eadp li\u1ec7u ho\u1eb7c d\u1eef li\u1ec7u thu th\u1eadp t\u1eeb web.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. M\u1ed9t s\u1ed1 l\u01b0u \u00fd v\u00e0 best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>N\u1ebfu ch\u1ec9 c\u1ea7n thao t\u00e1c \u0111\u01a1n gi\u1ea3n, h\u00e3y \u01b0u ti\u00ean d\u00f9ng <strong>ph\u01b0\u01a1ng th\u1ee9c chu\u1ed7i<\/strong> (<code>in<\/code>, <code>replace<\/code>, <code>find<\/code>) v\u00ec d\u1ec5 \u0111\u1ecdc v\u00e0 hi\u1ec7u n\u0103ng t\u1ed1t.<\/li>\n\n\n\n<li>Khi c\u1ea7n x\u1eed l\u00fd ph\u1ee9c t\u1ea1p theo m\u1eabu, <strong>regex<\/strong> l\u00e0 l\u1ef1a ch\u1ecdn m\u1ea1nh m\u1ebd nh\u01b0ng h\u00e3y c\u1ea9n th\u1eadn \u0111\u1ec3 kh\u00f4ng vi\u1ebft regex qu\u00e1 kh\u00f3 hi\u1ec3u.<\/li>\n\n\n\n<li>V\u1edbi d\u1eef li\u1ec7u l\u1edbn, n\u00ean c\u00e2n nh\u1eafc hi\u1ec7u n\u0103ng: tr\u00e1nh l\u1eb7p l\u1ea1i regex ph\u1ee9c t\u1ea1p qu\u00e1 nhi\u1ec1u l\u1ea7n.<\/li>\n\n\n\n<li>Lu\u00f4n ki\u1ec3m th\u1eed v\u1edbi d\u1eef li\u1ec7u th\u1ef1c t\u1ebf, \u0111\u1eb7c bi\u1ec7t khi regex c\u00f3 th\u1ec3 kh\u1edbp sai ngo\u00e0i d\u1ef1 ki\u1ebfn.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5. K\u1ebft lu\u1eadn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Trong su\u1ed1t b\u00e0i vi\u1ebft, m\u00ecnh v\u00e0 b\u1ea1n \u0111\u00e3 c\u00f9ng nhau \u0111i t\u1eeb nh\u1eefng thao t\u00e1c c\u01a1 b\u1ea3n nh\u01b0 <strong>in<\/strong>, <strong>find()<\/strong>, <strong>replace()<\/strong>, cho \u0111\u1ebfn nh\u1eefng c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd h\u01a1n nh\u01b0 th\u01b0 vi\u1ec7n <strong>re<\/strong> v\u1edbi <strong>search()<\/strong>, <strong>findall()<\/strong>, <strong>sub()<\/strong>. Kh\u00f4ng ch\u1ec9 d\u1eebng \u1edf \u0111\u00f3, ch\u00fang ta c\u00f2n ch\u1ea1m \u0111\u1ebfn m\u1ed9t v\u00e0i k\u1ef9 thu\u1eadt n\u00e2ng cao th\u00fa v\u1ecb nh\u01b0 d\u00f9ng <strong>callback function<\/strong>, x\u1eed l\u00fd <strong>kh\u00f4ng ph\u00e2n bi\u1ec7t hoa th\u01b0\u1eddng<\/strong>, hay th\u1eadm ch\u00ed l\u00e0 <strong>l\u00e0m s\u1ea1ch d\u1eef li\u1ec7u<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Khi \u0111\u00e3 n\u1eafm v\u1eefng nh\u1eefng ph\u01b0\u01a1ng ph\u00e1p n\u00e0y, vi\u1ec7c x\u1eed l\u00fd v\u0103n b\u1ea3n trong Python s\u1ebd tr\u1edf n\u00ean nh\u1eb9 nh\u00e0ng h\u01a1n r\u1ea5t nhi\u1ec1u. \u0110\u00e2y c\u0169ng l\u00e0 n\u1ec1n t\u1ea3ng quan tr\u1ecdng \u0111\u1ec3 b\u1ea1n \u00e1p d\u1ee5ng v\u00e0o c\u00e1c t\u00ecnh hu\u1ed1ng th\u1ef1c t\u1ebf nh\u01b0 <strong>ph\u00e2n t\u00edch d\u1eef li\u1ec7u<\/strong>, <strong>t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00f4ng vi\u1ec7c<\/strong>, ho\u1eb7c x\u00e2y d\u1ef1ng c\u00e1c <strong>h\u1ec7 th\u1ed1ng x\u1eed l\u00fd th\u00f4ng tin th\u00f4ng minh<\/strong>. N\u00f3i c\u00e1ch kh\u00e1c, ch\u1ec9 c\u1ea7n th\u00e0nh th\u1ea1o chu\u1ed7i, b\u1ea1n s\u1ebd c\u00f3 th\u00eam m\u1ed9t \u201cv\u0169 kh\u00ed\u201d c\u1ef1c m\u1ea1nh trong h\u00e0nh tr\u00ecnh l\u1eadp tr\u00ecnh c\u1ee7a m\u00ecnh.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. T\u00e0i li\u1ec7u tham kh\u1ea3o<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python Software Foundation. (2024). <em>str \u2014 Common string operations<\/em>. Python documentation. <a>https:\/\/docs.python.org\/3\/library\/stdtypes.html#string-methods<\/a><\/li>\n\n\n\n<li>Python Software Foundation. (2024). <em>re \u2014 Regular expression operations<\/em>. Python documentation. <a>https:\/\/docs.python.org\/3\/library\/re.html<\/a><\/li>\n\n\n\n<li>Friedl, J. E. F. (2006). <em>Mastering Regular Expressions<\/em> (3rd ed.). O\u2019Reilly Media.<\/li>\n\n\n\n<li>Python for Professionals: Learning Python as a Second Language: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710002222624882405978&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-for-professionals-3\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-for-professionals-3<\/a><\/li>\n\n\n\n<li>Python: Deeper Insights into Machine Learning: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710015810095319857183&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-deeper-insights-into-machine-learning\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-deeper-insights-into-machine-learning<\/a><\/li>\n\n\n\n<li>DataFusion Python Bindings in Practice: The Complete Guide for Developers and Engineers: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710049093362364820452&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fdatafusion-python-bindings-in-practice\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/datafusion-python-bindings-in-practice<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Trong Python, c\u00f3 r\u1ea5t nhi\u1ec1u c\u00e1ch \u0111\u1ec3 th\u1ef1c hi\u1ec7n t\u00ecm ki\u1ebfm v\u00e0 thay th\u1ebf chu\u1ed7i \u2013 t\u1eeb c\u00e1c ph\u01b0\u01a1ng th\u1ee9c chu\u1ed7i c\u01a1 b\u1ea3n cho \u0111\u1ebfn c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd h\u01a1n l\u00e0 Regular Expression (regex). B\u00e0i vi\u1ebft n\u00e0y m\u00ecnh s\u1ebd t\u1ed5ng h\u1ee3p l\u1ea1i c\u00e1c ph\u01b0\u01a1ng ph\u00e1p t\u1eeb \u0111\u01a1n gi\u1ea3n \u0111\u1ebfn n\u00e2ng cao, k\u00e8m v\u00ed d\u1ee5 minh h\u1ecda, \u0111\u1ec3 gi\u00fap vi\u1ec7c x\u1eed l\u00fd chu\u1ed7i tr\u1edf n\u00ean hi\u1ec7u qu\u1ea3 h\u01a1n.<\/p>\n","protected":false},"author":1,"featured_media":1641,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowieHDDA:productID":"","footnotes":""},"categories":[41],"tags":[40],"class_list":["post-1479","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kien-thuc-lap-trinh","tag-python-co-ban"],"_links":{"self":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/comments?post=1479"}],"version-history":[{"count":3,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1479\/revisions"}],"predecessor-version":[{"id":2435,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1479\/revisions\/2435"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media\/1641"}],"wp:attachment":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media?parent=1479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/categories?post=1479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/tags?post=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}