{"id":3944,"date":"2024-04-22T17:33:03","date_gmt":"2024-04-22T10:33:03","guid":{"rendered":"https:\/\/www.marketenterprise.vn\/blog\/?p=3944"},"modified":"2024-08-21T09:10:01","modified_gmt":"2024-08-21T02:10:01","slug":"regex-hoat-dong-ra-sao","status":"publish","type":"post","link":"https:\/\/www.marketenterprise.vn\/blog\/regex-hoat-dong-ra-sao.html","title":{"rendered":"Regex l\u00e0 c\u00e1i chi chi? N\u00f3 ho\u1ea1t \u0111\u1ed9ng ra sao?"},"content":{"rendered":"<h1 style=\"color: white;\">Regex ho\u1ea1t \u0111\u1ed9ng th\u1ebf n\u00e0o?<\/h1>\n<h2>Regex l\u00e0 g\u00ec? L\u1ee3i \u00edch c\u1ee7a n\u00f3?<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/04\/08195905\/regex-300x287.png\" alt=\"regex l\u00e0 g\u00ec?\" width=\"300\" height=\"287\" class=\"wp-image-3945 size-medium aligncenter\" srcset=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/04\/08195905\/regex-300x287.png 300w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2024\/04\/08195905\/regex.png 640w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Regex gi\u00fap ta thay \u0111\u1ed5i \u0111\u1ecbnh d\u1ea1ng s\u1ed1 \u0111i\u1ec7n tho\u1ea1i, t\u00ecm ki\u1ebfm t\u1eeb trong v\u0103n b\u1ea3n, x\u00e1c \u0111\u1ecbnh \u0111\u1ecbnh d\u1ea1ng \u0111\u1ea7u v\u00e0o cho c\u00e1c \u00f4 nh\u1eadp th\u00f4ng tin, tr\u00edch xu\u1ea5t th\u00f4ng tin v\u00e0 nhi\u1ec1u th\u1ee9 kh\u00e1c n\u1eefa.<\/p>\n<p><span style=\"font-weight: 400;\">Regex ho\u1eb7c RegExp l\u00e0 vi\u1ebft t\u1eaft c\u1ee7a Regular Expression (Bi\u1ec3u th\u1ee9c ch\u00ednh quy). \u0110\u01a1n thu\u1ea7n n\u00f3 l\u00e0 m\u1ed9t chu\u1ed7i c\u00e1c k\u00fd t\u1ef1 m\u00f4 t\u1ea3 nh\u1eefng g\u00ec ta mu\u1ed1n t\u00ecm ki\u1ebfm hay mu\u1ed1n tr\u00edch xu\u1ea5t. Nh\u01b0ng v\u1edbi chu\u1ed7i k\u00fd t\u1ef1 n\u00e0y th\u00ec kh\u00f4ng th\u1ec3 n\u00e0o t\u1ea1o n\u00ean \u0111i\u1ec1u \u0111\u1eb7c bi\u1ec7t, m\u00e0 ph\u1ea3i c\u00f3 m\u1ed9t c\u00f4ng c\u1ee5 \u0111\u1ec3 th\u1ef1c thi, ng\u01b0\u1eddi g\u1ecdi \u0111\u00f3 l\u00e0 \u201cRegular Expression engine\u201d. C\u00fa ph\u00e1p v\u00e0 h\u00e0nh vi x\u1eed l\u00fd c\u1ee7a m\u1ed9t Regular Expression <\/span><span style=\"font-weight: 400;\">engine<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 \u201cRegular expression flavor\u201d. V\u1edbi m\u1ed7i ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh, regular expression flavor s\u1ebd c\u00f3 \u0111\u00f4i ch\u00fat kh\u00e1c<\/span><span style=\"font-weight: 400;\"> nhau nh\u01b0ng<\/span><span style=\"font-weight: 400;\"> kh\u00f4ng nhi\u1ec1u.<\/span><\/p>\n<h3>C\u1ea7n nh\u1eefng g\u00ec \u0111\u1ec3 c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng \u0111\u01b0\u1ee3c Regex?<\/h3>\n<p><span style=\"font-weight: 400;\">Tr\u01b0\u1edbc ti\u00ean ph\u1ea3i n\u1eafm r\u00f5 c\u00e1ch th\u1ee9c m\u00e0 n\u00f3 ho\u1ea1t \u0111\u1ed9ng, sau \u0111\u00f3 l\u00e0 c\u00fa ph\u00e1p. V\u00ec v\u1eady, trong b\u00e0i vi\u1ebft n\u00e0y m\u00ecnh s\u1ebd t\u00ecm hi\u1ec3u s\u01a1 l\u01b0\u1ee3c c\u00e1ch m\u1ed9t Regex Engine ho\u1ea1t \u0111\u1ed9ng th\u00f4ng qua c\u00e1c v\u00ed d\u1ee5.<\/span><\/p>\n<h2>V\u00ed d\u1ee5 t\u00ecm ki\u1ebfm m\u1ed9t t\u1eeb trong \u0111o\u1ea1n v\u0103n:<\/h2>\n<p><span style=\"font-weight: 400;\">Tr\u01b0\u1edbc khi \u0111i v\u00e0o v\u00ed d\u1ee5 c\u1ee5 th\u1ec3, ch\u00fang ta s\u1ebd c\u00f3 m\u1ed9t v\u00e0i quy \u01b0\u1edbc \u0111\u1ec3 tr\u00e1nh g\u00e2y nh\u1ea7m l\u1eabn khi gi\u1ea3i th\u00edch v\u00ed d\u1ee5.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Chu\u1ed7i regex s\u1ebd c\u00f3 n\u1ec1n m\u00e0u xanh l\u00e1 <\/span><span style=\"font-weight: 400; background-color: #38761d; color: white;\">abcde<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">\u0110o\u1ea1n v\u0103n m\u1eabu c\u00f3 n\u1ec1n m\u00e0u v\u00e0ng <\/span><span style=\"font-weight: 400; background-color: #bf9000; color: white;\">abcde<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00e0o v\u00ed d\u1ee5 n\u00e0o.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ex1: T\u00ecm ki\u1ebfm <\/span><span style=\"font-weight: 400; background-color: #38761d; color: white;\">Cat<\/span><span style=\"font-weight: 400;\"> trong \u0111o\u1ea1n v\u0103n sau: <\/span><span style=\"font-weight: 400; background-color: #bf9000; color: white;\">Cats are animals<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Khi Regex engine b\u1eaft \u0111\u1ea7u th\u1ef1c thi, n\u00f3 s\u1ebd b\u1eaft \u0111\u1ea7u t\u1ea1i k\u00fd t\u1ef1 \u0111\u1ea7u ti\u00ean c\u1ee7a chu\u1ed7i regex (<span style=\"font-weight: 400; background-color: #38761d; color: white;\">C<\/span><\/span><span style=\"font-weight: 400;\">), v\u1edbi k\u00fd t\u1ef1 \u0111\u1ea7u ti\u00ean trong \u0111o\u1ea1n v\u0103n (<span style=\"font-weight: 400; background-color: #bf9000; color: white;\">C<\/span><\/span><span style=\"font-weight: 400;\">), \u201cC\u201d kh\u1edbp v\u1edbi \u201cC\u201d.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ti\u1ebfp t\u1ee5c l\u1ea5y k\u00fd t\u1ef1 th\u1ee9 hai trong (<span style=\"background-color: #38761d; color: white;\">a<\/span><\/span><span style=\"font-weight: 400;\">) so v\u1edbi k\u00fd t\u1ef1 th\u1ee9 hai trong \u0111o\u1ea1n v\u0103n (<span style=\"background-color: #bf9000; color: white;\">a<\/span><\/span><span style=\"font-weight: 400;\">), v\u00ec \u201ca\u201d kh\u1edbp v\u1edbi \u201ca\u201d, n\u00ean l\u1ea5y (<span style=\"background-color: #38761d; color: white;\">t<\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><span style=\"font-weight: 400;\">so s\u00e1nh v\u1edbi (<span style=\"background-color: #bf9000; color: white;\">t<\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"> trong \u0111o\u1ea1n v\u0103n. V\u1eady l\u00e0 \u0111\u00e3 t\u00ecm \u0111\u01b0\u1ee3c Cat trong \u0111o\u1ea1n v\u0103n, Regex engine s\u1ebd d\u1eebng l\u1ea1i t\u1ea1i \u0111\u00e2y<span style=\"font-weight: 400; color: red;\">(*)<\/span>.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ex2: T\u00ecm ki\u1ebfm <span style=\"font-weight: 400; background-color: #38761d; color: white;\">cat<\/span><\/span><span style=\"font-weight: 400;\">\u00a0trong \u0111o\u1ea1n v\u0103n sau: <\/span><span style=\"font-weight: 400; background-color: #bf9000; color: white;\">cars are not animals but cats are<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u1eabn nh\u01b0 \u1edf v\u00ed d\u1ee5 m\u1ed9t, Regex engine b\u1eaft \u0111\u1ea7u v\u1edbi k\u00fd t\u1ef1 \u0111\u1ea7u ti\u00ean (<span style=\"font-weight: 400; background-color: #38761d; color: white;\">c<\/span><\/span><span style=\"font-weight: 400;\">) so s\u00e1nh v\u1edbi k\u00fd t\u1ef1 \u0111\u1ea7u ti\u00ean trong \u0111o\u1ea1n v\u0103n (<span style=\"background-color: #bf9000; color: white;\">c<\/span><\/span><span style=\"font-weight: 400;\">), \u201cc\u201d kh\u1edbp v\u1edbi \u201cc\u201d.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ti\u1ebfp t\u1ee5c v\u1edbi k\u00fd t\u1ef1 th\u1ee9 hai trong (<\/span><span style=\"font-weight: 400;\"><span style=\"background-color: #38761d; color: white;\">a<\/span><\/span><span style=\"font-weight: 400;\">) so v\u1edbi k\u00fd t\u1ef1 th\u1ee9 hai trong \u0111o\u1ea1n v\u0103n (<\/span><span style=\"font-weight: 400;\"><span style=\"background-color: #bf9000; color: white;\">a<\/span><\/span><span style=\"font-weight: 400;\">), \u201ca\u201d kh\u1edbp v\u1edbi \u201ca\u201d.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ebfn k\u00fd t\u1ef1 th\u1ee9 3 (<\/span><span style=\"font-weight: 400;\"><span style=\"background-color: #38761d; color: white;\">t<\/span><\/span><span style=\"font-weight: 400;\">) k\u1ebft qu\u1ea3 l\u00e0 kh\u00f4ng kh\u1edbp v\u1edbi (<\/span><span style=\"font-weight: 400;\"><span style=\"font-weight: 400; background-color: #bf9000; color: white;\">r<\/span><\/span><span style=\"font-weight: 400;\">) trong \u0111o\u1ea1n v\u0103n. V\u00ec v\u1eady, t\u1ea1i b\u01b0\u1edbc k\u1ebf ti\u1ebfp Regex engine quay l\u1ea1i k\u00fd t\u1ef1 \u0111\u1ea7u ti\u00ean trong (<\/span><span style=\"font-weight: 400;\"><span style=\"font-weight: 400; background-color: #38761d; color: white;\">c<\/span><\/span><span style=\"font-weight: 400;\">) so s\u00e1nh v\u1edbi k\u00fd t\u1ef1 th\u1ee9 hai trong \u0111o\u1ea1n v\u0103n (<\/span><span style=\"font-weight: 400;\"><span style=\"background-color: #bf9000; color: white;\">a<\/span><\/span><span style=\"font-weight: 400;\">), \u201cc\u201d kh\u00f4ng kh\u1edbp v\u1edbi \u201ca\u201d.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ebfn k\u00fd t\u1ef1 th\u1ee9 3 (<\/span><span style=\"font-weight: 400;\">t<\/span><span style=\"font-weight: 400;\">) k\u1ebft qu\u1ea3 l\u00e0 kh\u00f4ng kh\u1edbp v\u1edbi (<\/span><span style=\"font-weight: 400;\">r<\/span><span style=\"font-weight: 400;\">) trong \u0111o\u1ea1n v\u0103n. V\u00ec v\u1eady, t\u1ea1i b\u01b0\u1edbc k\u1ebf ti\u1ebfp Regex engine quay l\u1ea1i k\u00fd t\u1ef1 \u0111\u1ea7u ti\u00ean trong (<span style=\"font-weight: 400; background-color: #38761d; color: white;\">c<\/span><\/span><span style=\"font-weight: 400;\">) so s\u00e1nh v\u1edbi k\u00fd t\u1ef1 th\u1ee9 hai trong \u0111o\u1ea1n v\u0103n (<\/span><span style=\"font-weight: 400;\">a<\/span><span style=\"font-weight: 400;\">), \u201cc\u201d kh\u00f4ng kh\u1edbp v\u1edbi \u201ca\u201d.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">L\u1ea1i l\u1ea5y (<span style=\"font-weight: 400; background-color: #38761d; color: white;\">c<\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"> so s\u00e1nh v\u1edbi (<span style=\"color: #ffffff;\"><span style=\"background-color: #bf9000;\">r<\/span><\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"> k\u1ebft qu\u1ea3 kh\u00f4ng kh\u1edbp nhau. V\u00e0 ti\u1ebfp t\u1ee5c nh\u01b0 th\u1ebf so s\u00e1nh k\u00fd t\u1ef1 (<span style=\"font-weight: 400; background-color: #38761d; color: white;\">c<\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\">\u00a0v\u1edbi t\u1eebng k\u00fd t\u1ef1 ti\u1ebfp theo trong \u0111o\u1ea1n v\u0103n, cho \u0111\u1ebfn khi (<span style=\"font-weight: 400; background-color: #38761d; color: white;\">c<\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"> kh\u1edbp v\u1edbi k\u00fd t\u1ef1 (<span style=\"color: #ffffff;\"><span style=\"background-color: #bf9000;\">c<\/span><\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"> t\u1ea1i v\u1ecb tr\u00ed 26 trong \u0111o\u1ea1n v\u0103n. Sau khi \u201cc\u201d tr\u00f9ng kh\u1edbp v\u1edbi \u201cc\u201d, sau \u0111\u00f3 ta l\u1ea5y (<span style=\"color: #ffffff;\"><span style=\"background-color: #38761d;\">a<\/span><\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"> so s\u00e1nh v\u1edbi (<span style=\"background-color: #bf9000; color: white;\">a<\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"> t\u1ea1i v\u1ecb tr\u00ed th\u1ee9 27, \u201ca\u201d kh\u1edbp v\u1edbi \u201ca\u201d. Regex engine so ti\u1ebfp (<span style=\"color: #ffffff;\"><span style=\"background-color: #38761d;\">t<\/span><\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"> v\u1edbi (<span style=\"color: #ffffff;\"><span style=\"background-color: #bf9000;\">t<\/span><\/span><\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\">, \u201ct\u201d kh\u1edbp v\u1edbi \u201ct\u201d. V\u1eady l\u00e0 ta \u0111\u00e3 t\u00ecm \u0111\u01b0\u1ee3c \u201ccat\u201d trong \u0111o\u1ea1n v\u0103n. \u0110\u1ebfn \u0111\u00e2y, Regex engine d\u1eebng t\u00ecm ki\u1ebfm<span style=\"font-weight: 400; color: red;\">(*)<\/span><\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ch\u00fa \u00fd:\u00a0<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\"><span style=\"font-weight: 400; color: red;\">(*)<\/span> Gi\u1ea3 s\u1eed v\u1eabn c\u00f2n t\u1eeb<\/span><span style=\"font-weight: 400;\"> \u201cCat\u201d<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c<\/span><span style=\"font-weight: 400;\"> \u201ccat\u201d<\/span><span style=\"font-weight: 400;\"> kh\u00e1c trong \u0111o\u1ea1n v\u0103n tr\u00ean<\/span><span style=\"font-weight: 400;\"> v\u00e0<\/span><span style=\"font-weight: 400;\"> b\u1ea1n mu\u1ed1n t\u00ecm ki\u1ebfm ti\u1ebfp nh\u1eefng \u201c<\/span><span style=\"font-weight: 400;\">Cat\u201d <\/span><span style=\"font-weight: 400;\">ho\u1eb7c<\/span><span style=\"font-weight: 400;\"> \u201ccat\u201d<\/span><span style=\"font-weight: 400;\"> c\u00f2n l\u1ea1i th\u00ec ta s\u1ebd t\u00ecm hi\u1ec3u trong b\u00e0i vi\u1ebft li\u00ean quan \u0111\u1ebfn <strong>Regex modifiers.<\/strong><\/span><span style=\"font-weight: 400;\"><\/span><\/li>\n<li><span style=\"font-weight: 400;\">Kho\u1ea3ng tr\u1ed1ng v\u1eabn \u0111\u01b0\u1ee3c so s\u00e1nh trong hai v\u00ed d\u1ee5 tr\u00ean.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u1edbi hai v\u00ed d\u1ee5 tr\u00ean ch\u1eafc ph\u1ea7n n\u00e0o b\u1ea1n \u0111\u00e3 h\u00ecnh dung c\u00e1ch ho\u1ea1t \u0111\u1ed9ng ra sao? Trong nh\u1eefng b\u00e0i ti\u1ebfp theo ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 nh\u1eefng c\u00fa ph\u00e1p c\u01a1 b\u1ea3n s\u1eed d\u1ee5ng trong Regex.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u1ea3m \u01a1n b\u1ea1n \u0111\u00e3 \u0111\u1ecdc b\u00e0i vi\u1ebft c\u1ee7a m\u00ecnh, r\u1ea5t mong nh\u1eefng ph\u1ea3n h\u1ed3i t\u1eeb c\u00e1c b\u1ea1n (n\u1ebfu c\u00f3) \u0111\u1ec3 c\u00f9ng nhau hi\u1ec3u h\u01a1n v\u1ec1 <\/span><span style=\"font-weight: 400;\">Regular Expression (Bi\u1ec3u th\u1ee9c ch\u00ednh quy).<\/span><\/p>\n<p>Ngu\u1ed3n tham kh\u1ea3o: <a href=\"https:\/\/www.regular-expressions.info\/engine.html\" target=\"_blank\" rel=\"noopener\">https:\/\/www.regular-expressions.info\/engine.html<\/a><\/p>\n<p>Ngo\u00e0i ra, c\u00f2n nhi\u1ec1u b\u00e0i vi\u1ebft k\u1ef9 thu\u1eadt t\u1eeb <a href=\"https:\/\/www.marketenterprise.vn\/blog\/\">MEVN blogs<\/a> m\u1eddi c\u00e1c b\u1ea1n t\u00ecm \u0111\u1ecdc.<\/p>\n<div id=\"eJOY__extension_root\" class=\"eJOY__extension_root_class\" style=\"all: unset;\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>T\u00ecm hi\u1ec3u v\u1ec1 c\u00e1ch th\u1ee9c ho\u1ea1t \u0111\u1ed9ng c\u1ee7a bi\u1ec3u th\u1ee9c ch\u00ednh quy(Regex).<\/p>\n","protected":false},"author":37,"featured_media":3945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3944","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/3944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/comments?post=3944"}],"version-history":[{"count":0,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/3944\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media\/3945"}],"wp:attachment":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media?parent=3944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/categories?post=3944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/tags?post=3944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}