ในงาน NLP เรามักจะต้องเจอกับ การจัดการ String Format ที่ค่อนข้างซับซ้อน เช่น หาอีเมล์ หาเบอร์โทรศัพท์ หาลิงค์ ถ้าเราต้องเขียนโปรแกรม if else วนลูป เช็คเองทุกเคส โปรแกรมจะซับซ้อนมาก วิธีแก้ที่เป็นที่นิยม คือ ใช้ Regular Expression (RegEx, RegExp, RE)

Regular Expression, RegEx คืออะไร

Regular Expressions https://xkcd.com/208/
Regular Expressions. Credit https://xkcd.com/208/

Regular Expression, Regex หรือ Regexp คือ ลำดับอักษร Pattern ที่ใช้ในการค้นหาข้อความใน String โดยสามารถใช้ค้นหาอย่างเดียว, ค้นหาแล้วแทนที่ด้วยข้อความที่กำหนดก็ได้ หรือใช้ตรวจสอบ String Input ว่าเป็นไปตาม Format ที่กำหนดหรือไม่

Regular Expression เป็นที่นิยมมาก เนื่องจากแก้ปัญหาที่ทุกคนต้องเจอ ซ้ำ ๆ อย่างมีประสิทธิภาพ เราจะพบ Regular Expresion ได้ในทุกภาษา Programming, Tools, Editor ชั้นนำ แต่ Pattern RegEx ที่ใช้อาจจะต่างกันเล็กน้อย

เรามาเริ่มกันเลยดีกว่า

Open In Colab

แชร์ให้เพื่อน:

Surapong Kanoktipsatharporn on FacebookSurapong Kanoktipsatharporn on LinkedinSurapong Kanoktipsatharporn on Rss
Surapong Kanoktipsatharporn
Solutions Architect at Bua Labs
The ultimate test of your knowledge is your capacity to convey it to another.

Published by Surapong Kanoktipsatharporn

The ultimate test of your knowledge is your capacity to convey it to another.