ThepExcel Logo
  • บทความ
    • Excel
      • Excel ทั่วไป
      • Excel Pivot Table
      • Excel Power Pivot
      • Power Query
      • Excel Array Formula
      • Excel VBA
      • Excel for Business
      • Excel and Maths
      • ฟังก์ชัน Excel ทั้งหมด
    • Power BI
      • Power Query
      • Data Model
      • DAX Formula
      • Power BI Report
    • Coding
      • Excel VBA
      • Python
      • Power Query M Code
    • AI
      • ChatGPT
      • Stable Diffusion
      • MidJourney
    • Highlights : บทความแนะนำ
    • คลิปวีดีโอ
  • อบรม
    • อบรมลูกค้าองค์กร
    • คอร์สออนไลน์ SkillLane
    • แนะนำวิทยากร
    • Excel/Power BI Skill Map
    • Quiz
  • Shop
    • คอร์สออนไลน์
    • สินค้าทั้งหมด
    • หนังสือเล่ม
    • E-Book
    • Cart
  • Download
    • Download ไฟล์จากเทพเอ็กเซล
    • ThepExcel-Mfx : M Code สำเร็จรูป
    • Date Table สำเร็จรูป
    • กราฟ My Skill
    • github.com/ThepExcel
  • รวม Link
    • รวม Link สอน Excel & Power BI ทั้งไทยและเทศ
    • รวม Link เกี่ยวกับ AI
    • รวม Link Coding
    • หนังสือแนะนำ
    • Facebook ThepExcel
    • YouTube ThepExcel
    • DAX Formatter
  • Contact
    • แนะนำ เทพเอ็กเซล (Thep Excel)
    • แนะนำวิทยากร : อาจารย์ ศิระ เอกบุตร (ระ)
    • นโยบายการคุ้มครองข้อมูลส่วนบุคคล (Privacy Policy)
  • Facebook
  • YouTube

ค้นหาข้อความที่มีลักษณะตามต้องการ ด้วย Regular Expression (Regex)เบื้องต้น

regular expression regex

Categories 📂

Programming

Tags 🏷️

regex

สิ่งหนึ่งที่เป็นจุดอ่อนของ Excel ในปัจจุบัน ก็คือความสามารถในการหาข้อความที่มี Pattern ตามต้องการ ใน Excel มีแค่ Wildcard (* กับ ?) ซึ่งไม่ค่อยยืดหยุ่นเท่าไหร่ โดยเฉพาะถ้าเอาไปเทียบกับการใช้ Regular Expression (เรียกสั้นๆว่า Regex) ซึ่งเป็นวิธีที่ตอบโจทย์กว่าการใช้สัญลักษณ์ Wildcard ใน Excel อย่างเทียบไม่ติด

ถ้าเราใช้ Regular Expression เป็น เราก็จะสามารถค้นหาข้อมูลที่มีลักษณะตามรูปแบบ (Pattern) ตามที่เรากำหนดได้ เช่น จะหา email, เบอร์โทรศัพท์, ชื่อ นามสกุล หรืออะไรก็ตามในข้อความ ขอแค่มันมี Pattern ที่แน่นอนก็พอ

ที่น่าคับแค้นใจคือในโปรแกรมอย่าง Google Sheets หรือ Python สามารถดรียกใช้ Regex ได้ค่อนข้างสะดวก แต่ถ้าจะใช้ Regex ใน Excel เราจะต้อง import Library พิเศษผ่าน VBA ก่อน (ต้อง Add Reference ชื่อ Microsoft VBScript Regular Expressions) ซึ่งเป็นเรื่องที่ค่อนข้างยุ่งยากสำหรับคนทั่วไป ดังนั้นใครอยากให้ Excel มี Regex ในตัวเลยโดยไม่ต้องใช้ VBA สามารถไป Vote ได้ที่นี่

ในบทความนี้จะเป็นการสอนใช้ Regular Expression เบื้องต้น ซึ่งความรู้นี้สามารถนำไปใช้ได้กับหลายโปรแกรมเลยที่รองรับ Regular Expression ดังนั้นเรียนรู้ไปแล้วคุ้มแน่นอน (เดี๋ยวเราจะมาทำ Regex กันจริงๆ ใน Excel VBA, Google Sheets, Python ในตอนต่อไปนะครับ)

สารบัญ

  • เริ่มเรียนรู้ Regular Expression
    • Syntax พื้นฐาน
    • ตัวอย่าง Pattern และความหมาย
    • รูปแบบที่ซับซ้อนขึ้น
  • ตอนต่อไป

เริ่มเรียนรู้ Regular Expression

วิธีการเรียนรู้ Regular Expression ได้จากหลายเว็บ เช่น

  • https://regexone.com/
  • https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285
  • https://cheatography.com/davechild/cheat-sheets/regular-expressions/

แต่ถ้าใครขี้เกียจอ่านในเว็บอื่น ลองอ่านที่ผมสรุปเบื้องต้นข้างล่างนี้ก็ได้

พอเรียนแล้ว สามารถลองเอานำคำที่เราต้องการ Match ไปลองใส่ในเว็บ https://regexr.com/ แล้วลองใส่ pattern ต่างๆ ลงไปดูว่ามันเจออะไรบ้าง ข้อดีคือนอกจากมันจะตอบอบ่างรวดเร็วว่าเจออะไรบ้าง ตรงไหน มันยังอธิบาย Pattern ที่เราเขียนให้ด้วยว่าหมายความว่ายังไง

Syntax พื้นฐาน

abcกขค…	ตัวหนังสือ(ที่ระบุ)
123…	ตัวเลข(ที่ระบุ)
\d	ตัวเลขอะไรก็ได้
\D	อักขระอะไรก็ได้ที่ไม่ใช่ตัวเลข
.	อักขระอะไรก็ได้
\.	เครื่องหมายจุด
[abc]	a, b, หรือ c
[^abc]	ไม่ใช่ a, b, c
[a-z]	a ถึง z ตัวไหนก็ได้
[0-9]	เลข 0 ถึง 9
\w	อักขระภาษาอังกฤษหรือตัวเลข
\W	ไม่ใช่อักขระภาษาอังกฤษหรือตัวเลข
{m}	มีจำนวน m ครั้ง
{m,n}	มีจำนวน m-n ครั้ง
*	มีกี่ตัวก็ได้หรือไม่มีก็ได้
+	มีอย่างน้อย 1 ตัว
?	มี 0 หรือ 1 ตัว
\s	เว้นวรรค (whitespace character)
\S	อะไรก็ได้ที่ไม่ใช่เว้นวรรค
^…	เริ่มด้วย…
…$	จบด้วย…
(…)	จัดกลุ่ม
(a(bc))	กลุ่มซ้อนกลุ่ม
(abc|def)	หา abc หรือ def

ตัวอย่าง Pattern และความหมาย

  • thep = มีคำว่า thep ตรงไหนก็ได้ในข้อความ
  • ^thep = ขึ้นต้นด้วย thep (คำว่า thep ต้องอยู่หน้าสุด)
  • thep$ = จบด้วยคำว่า thep (คำว่า thep ต้องอยู่หลังสุด)
  • ^thep$ = เป็นคำว่า thep เป๊ะๆ ห้ามมีคำอื่น
  • [thep] = มีตัว t h e หรือ p
  • thep[1-5] = มีคำว่า thep ตามด้วยเลข 1-5 (ตัวเดียว)
  • thep\d = มีคำว่า thep ตามด้วยเลข 0-9 (ตัวเดียว)
  • thep\d{5} = มีคำว่า thep ตามด้วยเลข 0-9 (5 digit)
  • thep\d{5,7} = มีคำว่า thep ตามด้วยเลข 0-9 (5-7 digit)
  • thep\d{5,} = มีคำว่า thep ตามด้วยเลข 0-9 (5 digit ขึ้นไป)
  • thep\D = มีคำว่า thep ตามด้วยอะไรก็ได้ที่ไม่ใช่ตัวเลข 1 ตัว
  • thep. = มี thep ตามด้วยตัวอะไรก็ได้ 1 ตัว
  • thep\. = มีคำว่า thep. (thepตามด้วยจุด) อยู่ในข้อความ
  • th.p = มี th ตามด้วยตัวอะไรก็ได้ 1 ตัว แล้วตามด้วย p
  • th.+p = มี th ตามด้วยตัวอะไรก็ได้อย่างน้อย 1 ตัว แล้วตามด้วย p
  • th.?p = มี th ตามด้วยตัวอะไรก็ได้ 0 หรือ 1 ตัว (มีหรือไม่มีก็ได้) แล้วตามด้วย p
  • th.*p = มี th ตามด้วยตัวอะไรก็ได้กี่ตัวก็ได้ (มีหรือไม่มีก็ได้) แล้วตามด้วย p
  • thep? = มีคำว่า the ตามด้วย p หรือไม่ก็ได้
  • thep+ = มีคำว่า the ตามด้วย p อย่างน้อย 1 ตัว
  • th(ep)+ = มีคำว่า th ตามด้วย ep อย่างน้อย 1 ชุด
  • (thep|inw)excel = มีคำว่า thepexcel หรือ inwexcel

รูปแบบที่ซับซ้อนขึ้น

สมมติข้อความที่ผมสนใจคืออันนี้ และผมต้องการดึงเฉพาะส่วนที่เป็น email ออกมา ดูสิว่าจะทำยังไง?

สวัสดี ผมชื่อ นายศิระ เอกบุตร ชื่อเล่น ระ อายุ 37 ปี สูง 170 cm มี email คือ thepexcel@gmail.com ชอบการ์ตูนเรื่อง Hunter x Hunter, Attack on Titan, Evangelion ชอบหนังเรื่อง The Matrix, Inception ชอบเล่นเกม ROV, Valorant, StarCraft2 ลืมบอกไปว่าผมเป็นคนทำเว็บ www.thepexcel.com เพื่อแบ่งปันความรู้เรื่อง Excel & Power BI  ยินดีที่ได้รู้จักครับ

การจะเลือกเอาสิ่งที่ต้องการ เราก็ต้องถามตัวเองว่า ถ้าเรามองด้วยตาเปล่า เรารู้ได้ยังไงว่าส่วนไหนของ email?

ถ้าถามผม มันต้องเป็นประมาณนี้

มีอักขระจำนวนนึง ตามด้วยเครื่องหมาย @ แล้วต่อด้วยอักขระอักจำนวนนึง ตามด้วยจุด แล้วตามอักขระอีกจำนวนนึง โดยต้องมีเครื่องหมายเว้นวรรคมาครอบทั้งหน้าหลังด้วย

เครื่องหมายที่เกี่ยวข้อง เพื่อที่อยากจะได้ email ตั้งแต่ @ จนถึง .

@.+\.
  • @ คือ ตัว @ (เราอยากจะหาตัวอะไรก็พิมพ์ตัวนั้น ยกเว้นจะไปซ้ำกับสัญลักษณ์พิเศษ ซึ่งต้องใส่ \ นำหน้าจะได้รู้ว่าเป็นอักขระธรรมดา)
  • .+ คืออักขระอะไรก็ได้อย่างน้อย 1 ตัว
    • . แทนอักขระอะไรก็ได้ (ที่ไม่ใช่ขึ้นบรรทัดใหม่)
    • + คือมีอย่างน้อย 1 ตัว
  • \. แทนจุด (\ คือ ใส่ไปให้รู้ว่าต้องการ . ธรรมดา ไม่ใช่สัญลักษณ์พิเศษ)

ปรากฏว่ามัน Match ข้อมูลเยอะมาก เพราะเราดันบอกว่าให้เอาตั้งแต่เครื่องหมาย @ แล้วตามด้วยตัวอะไรก็ได้อย่างน้อย 1 ตัว (เพราะใช้+) แล้วก็ตามด้วยด้วยจุด มันก็เล่น Match ให้เยอะที่สุดเท่าที่จะ Match ได้เลย อันนี้คือธรรมชาติของมันที่เรียกว่า Greedy Match (Match แบบตะกละ)

ถ้าเราต้องการให้มันหาให้น้อยที่สุดเท่าที่จำเป็น เราสามารถใส่เครื่องหมาย ? ต่อท้าย + เข้าไป (เราใส่ ? ต่อท้ายพวก * + ? ได้ เพื่อให้มันทำการหาแบบ Non Greedy )ซึ่งจะได้ผลว่าเจอถึงจุดตัวแรกที่ต่อจากเครื่องหมาย @ เท่านั้น (เพราะหาแบบไม่ตะกละแล้ว)

@.+?\.

อย่างไรก็ตามเราสามารถใช้เทคนิคอื่นได้ เช่น

  • \s คือ เว้นวรรค
  • \S คือ ทุกอย่างที่ไม่ใช่เว้นวรรค

ดังนั้นผมสามารถแก้ Pattern เป็นแบบนี้ได้ โดยที่ไม่ต้องใช้ ? เลย เพราะผมไม่เอาเว้นวรรค มันเลยไม่ข้ามไปที่คำอื่น(ข้ามไม่ได้เพราะมีเว้นวรรคคั่นอยู่)

@\S+\.

พอรู้แบบนี้ก็จะสามารถใช้ Pattern นี้เพื่อเอา Email ทั้งอันได้

\S+@\S+\.\S+

ถ้าในข้อความมี email หลายที่มันก็จะสามารถหาเจอได้ เช่น ข้อความกลายเป็นแบบนี้ ก็ไม่มีปัญหา

xyz@gmail.com สวัสดี ผมชื่อ นายศิระ เอกบุตร ชื่อเล่น ระ อายุ 37 ปี สูง 170 cm  abc@outlook.com เป็นคนทำเว็บ www.thepexcel.com เพื่อแบ่งปันความรู้เรื่อง Excel & Power BI มี email คือ thepexcel@gmail.com ชอบการ์ตูนเรื่อง Hunter x Hunter, Attack on Titan, Evangelion xxxx@aaa.co.th ชอบหนังเรื่อง The Matrix, Inception ชอบเล่นเกม ROV, Valorant, StarCraft2 ยินดีที่ได้รู้จักครับ thepexceltraining@gmail.com

ถ้าหากเราอยากจะได้ส่วนข้างหน้า @ และหลัง @ แยกกัน เราสามารถใส่วงเล็บเข้าไปแบ่ง Group ผลลัพธ์ได้ ดังนี้

(\S+)@(\S+\.\S+)

หรือจะใส่วงเล็บใหญ่ครอบไปอีกที เพื่อเอาทั้ง email เป็นอีก Group ด้วยก็ได้ดังนี้

((\S+)@(\S+\.\S+))
regular expression regex

ตอนต่อไป

ในตอนนี้เราได้เรียนรู้ทฤษฎีการใช้ Regular Expression เบื้องต้นกันไปแล้ว ตอนต่อไปจะเป็นตัวอย่างการใช้ Regular Expression กันใน Excel VBA, Google Sheets, Python จริงๆ ซักที รอติดตามได้เลยว่าจะใช้แบบไหนได้บ้างนะครับ

แชร์ความรู้ให้เพื่อนๆ ของคุณ
427    
427    

ติดตามเทพเอ็กเซล

  • Facebook
  • YouTube

อบรมกับเทพเอ็กเซล

🔥 คอร์สใหม่ล่าสุด 🔥

การทำ Optimization ด้วย Excel Solver
สำหรับงานวางแผน
คอร์สออนไลน์ เทพเอ็กเซล
คอร์สออนไลน์ จากเทพเอ็กเซล ดูกี่รอบก็ได้
อบรม Excel / Power BI ให้องค์กรของคุณ

บทความล่าสุด

  • วิธีผสม Model สูตรผสมแบบโดนใจใน Stable Diffusion [Part7]
  • สอนทำรูปตัวเองคู่กับสาว ใน Stable Diffusion [Part6]
  • สอน Train Model ตัวเองใน Stable Diffusion [Part5]
  • 10 Levels การเขียนสูตร Excel ในยุคใหม่
  • บันได 10 ขั้น เริ่มเรียนรู้ M Code ขุมพลังของ Power Query
  • แนวทางฝึกฝน Excel ให้เก่งขึ้น
  • รวม Link เว็บ/เพจเกี่ยวกับ AI

บทความแนะนำ

🔥ฟังก์ชันทั้งหมดใน Excel 🔥

  • 10 Levels การเขียนสูตร Excel ในยุคใหม่
  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 1
  • Series สอนดึงข้อมูลจากเว็บ ด้วย Power Automate Desktop
  • สรุปการใช้ LAMBDA ฟังก์ชันที่ใช้สร้างฟังก์ชันใน Excel 365 และผองเพื่อน
  • วิธีใช้ Excel คำนวณระยะเวลาการทำงานรวม แถมระบุเวลาพักได้แบบยืดหยุ่น
  • วิธีจัดการข้อมูลแย่ๆ ด้วย Power Query ทั้งข้อมูลปนกัน ข้อมูลอยู่บนหัวตาราง

Categories

Tags

ai collection concepts copy database Data Model data validation date dax dropdown error excel filter finance find format formula function game graph IF index intro len link logic lookup match matrix mcode m code merge mid overview paste pivot power query row sort speed split stable diffusion substitute table text time tips trim vba vlookup

Archives

  • April 2023 (8)
  • March 2023 (2)
  • February 2023 (2)
  • January 2023 (1)
  • October 2022 (1)
  • September 2022 (3)
  • August 2022 (3)
  • July 2022 (1)
  • June 2022 (3)
  • May 2022 (1)
  • April 2022 (2)
  • February 2022 (1)
  • December 2021 (2)
  • November 2021 (10)
  • September 2021 (2)
  • August 2021 (6)
  • July 2021 (2)
  • June 2021 (2)
  • May 2021 (10)
  • April 2021 (3)
  • March 2021 (3)
  • February 2021 (4)
  • January 2021 (8)
  • December 2020 (5)
  • November 2020 (13)
  • October 2020 (5)
  • September 2020 (11)
  • August 2020 (4)
  • July 2020 (13)
  • June 2020 (17)
  • May 2020 (16)
  • April 2020 (16)
  • March 2020 (10)
  • February 2020 (15)
  • January 2020 (16)
  • December 2019 (4)
  • November 2019 (3)
  • October 2019 (9)
  • September 2019 (1)
  • August 2019 (7)
  • June 2019 (3)
  • May 2019 (9)
  • April 2019 (9)
  • March 2019 (2)
  • February 2018 (1)
  • January 2018 (3)
  • November 2017 (3)
  • August 2017 (1)
  • July 2017 (1)
  • June 2017 (1)
  • May 2017 (6)
  • April 2017 (6)
  • March 2017 (7)
  • February 2017 (1)
  • January 2017 (2)
  • December 2016 (1)
  • October 2016 (2)
  • September 2016 (3)
  • August 2016 (2)
  • July 2016 (2)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (1)
  • March 2016 (2)
  • February 2016 (1)
  • January 2016 (2)
  • December 2015 (2)
  • November 2015 (5)
  • October 2015 (3)
  • June 2015 (2)
  • May 2015 (1)
  • April 2015 (26)
  • January 2015 (1)
  • December 2014 (1)
  • November 2014 (2)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • June 2014 (1)
  • May 2014 (1)
  • April 2014 (3)
  • March 2014 (3)
  • February 2014 (12)
  • January 2014 (7)
  • December 2013 (2)
  • November 2013 (8)
  • October 2013 (2)

เทพเอ็กเซล : Thep Excel

copyright © 2022

  • Facebook
  • YouTube
เว็บไซต์นี้ใช้คุกกี้ (Cookies)
บริษัท เทพเอ็กเซล จำกัด ให้ความสำคัญต่อข้อมูลส่วนบุคคลของท่าน เพื่อการพัฒนาและปรับปรุงเว็บไซต์รวมถึงสินค้าและบริการต่างๆ หากท่านใช้บริการเว็บไซต์นี้ โดยไม่มีการปรับตั้งค่าใดๆ แสดงว่าท่านยินยอมที่จะรับคุกกี้บนเว็บไซต์ และนโยบายสิทธิส่วนบุคคลของเรา
ตั้งค่าคุกกี้ยอมรับทั้งหมดอ่านเพิ่มเติม
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT