By SitP on Tag:

Regular Expression

หรือ บางที่เรียกสั้นๆว่า regex คือ แบบ หรือ รูปแบบ ที่สามารถจับคู่ กับบางส่วนหรือทั้งหมดของข้อความที่เราสนใจ ตัวอย่างเช่น ถ้าเรามีข้อความ "711 Seven" ข้อความนี้ เราสามารถบอกได้ไหมครับว่ารูปแบบของข้อความนี้คืออะไร เรามาดูตัวอย่างรูปแบบที่น่าจะเป็นไปได้

รูปแบบที่ 1: "711 Seven" กล่าวได้ว่าเป็นรูปแบบตรงตัวเลย
ข้อความนี้เริ่มต้นด้วยตัวเลข 3 ตัวคือ 711 จากนั้นตามด้วยเว้นวรรคและตามด้วยคำว่า Seven ซึ่งถ้าเราต้องการจับคู่ เราก็สามารถกำหนดรูปแบบให้เหมือนกันเป๊ะ กับรูปแบบที่เราต้องการ
เราลองมาดูตัวอย่าง code python ที่ใช้
{syntaxhighlighter SPEC}
D:\wpatrProject>ipy
IronPython 2.6.1 (2.6.10920.0) on .NET 4.0.30319.235
Type "help", "copyright", "credits" or "license" for more information.

import re #นำ module re คือ regular expression module มาใช้
pat = '711 Seven' #รูปแบบที่ต้องการใส่ไว้ในตัวแปร pat
myinput = "711 Seven Ho He Ho 534 Seven and Chiyo 711 Seven"
re.findall(pat,myinput)
['711 Seven', '711 Seven']
type(re.findall(pat3,myinput))
<type 'list'>

{/syntaxhighlighter}
method findall คือ หาข้อมูลที่ตรงกับรูปแบบที่ต้องการ แล้ว return ผลลัพธ์ ออกมาเป็น list

รูปแบบที่ 2: คือ ตัวเลข ช่องว่าง แล้วตามด้วยข้อความ
ใน regex ตัวเลขตัวเดียวจะใช้รูปแบบ \d มองว่า มันเป็น 1 digit เช่น 5
ตัวเลขอย่างน้อยหนึ่งตัวใช้รูปแบบ \d+ เช่น 523
และสุดท้าย \d. โดย pattern นี้จะมองตัวแรกเป็นตัวเลขหนึ่งตัวส่วนตัวที่สองเป็นอะไรก็ได้ที่ไม่ใช้\n หรือ new line
{syntaxhighlighter SPEC}

myinput = "711 Seven Ho He Ho 534 Seven and Chiyo 711 Seven"
pat4 = '\d'
re.findall(pat4,myinput)
['7', '1', '1', '5', '3', '4', '7', '1', '1']

pat6 ='\d+'
re.findall(pat6,myinput)
['711', '534', '711']

pat2 = r'\d+\sSeven'
re.findall(pat2,myinput)
['711 Seven', '534 Seven', '711 Seven']

e = '\d.'
myinput2 = "711 Seven Ho He Ho 534 Seven and Chiyo 756998.11 Seven"
re.findall(e,myinput2) # Test
['71', '1 ', '53', '4 ', '75', '69', '98', '11']
#ตัว pattern \d. จะวิ่งเปรียบเทียบกับ myinput2 ไปเรื่อยๆ เริ่มตั้งแต่ 71 1\s se ve n\s จนถึง en ตัวสุดท้าย ถ้า match ก็จะนำมาเก็บลง list ที่เป็นผลลัพธ์แล้วนำมาแสดง

myinput2 = "711 Seven Ho He Ho 534 Seven and Chiyo 75694.11 Seven"
re.findall(e,myinput2) # Test
['71', '1 ', '53', '4 ', '75', '69', '4.', '11']

myinput2 = "711 Seven Ho He Ho 534 Seven and Chiyo 75699g.11 Seven"
re.findall(e,myinput2) # Test
['71', '1 ', '53', '4 ', '75', '69', '9g', '11']

myinput2 = "711 Seven Ho He Ho 534 Seven and Chiyo 75699g.1\n Seven"
re.findall(e,myinput2) # Test
['71', '1 ', '53', '4 ', '75', '69', '9g']

myinput2 = "711 Seven Ho He Ho 534 Seven and Chiyo 75699g.1 Seven"
re.findall(e,myinput2) # Test
['71', '1 ', '53', '4 ', '75', '69', '9g', '1 ']

{/syntaxhighlighter}
จาก รูปแบบ \d. จะได้ ผลลัพธ์แค่ 2 ตัว คือขึ้นต้นด้วยตัวเลข และ อะไรก็ได้ที่ไม่ใช้ การขึ้นบรรทัดใหม่ (\n)
จะเห็นจากตัวอย่าง ตัวเลข '1 ' นั่นคือ เลข 1 ตามด้วย space ซึ่ง space นั้น regex มองว่ามันคือ \s

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

อันนี้น่าจะเป็น Tutorial มากกว่าข่าวนะครับ

ผมว่าน่าจะหาที่โพสท์ที่เกี่ยวข้องไปเลย เช่นที่ Codenone ครับ (ยังอยู่มั้ย?)

Python เขียน Regex แบบ Perl ได้เปล่าครับ? ผมไม่เห็นมี Delimiter แบบของ Perl เลย ผมว่าตามตัวอย่างน่าเป็นแบบ Posix

ผมยังไม่เคยศึกษา Python นะ แต่ผมว่า Regex เขียนแบบ Perl มันดีกว่านะ

ปล. ผมว่าไม่น่าขึ้นหน้าหนึ่งนะ Blognone เว็บข่าวนิครับ ไม่ใช่เว็บรวม Tutorial นะ