ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)
Moderators: mindphp, ผู้ดูแลกระดาน
-
- PHP Super Hero Member
- Posts: 604
- Joined: 07/12/2016 10:55 am
ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)
ฟังก์ชัน Search จะอยู่ในโมดูล re ใช้สำหรับค้นหาข้อความตามรูปแบบ (Patterns (แพทเทิล)) ที่กำหนด โดยจะคืนค่ากลับมาเป็น Object (อ๊อปเจค) ถ้าเจอข้อความ และคืนค่า None (นัน) ถ้าไม่เจอ
ข้อแตกต่างระหว่างฟังก์ชัน re.match (รี.แมท) และ re.search (รีเสริด)
re.match เริ่มตรวจสอบรูปแบบของข้อความตั้งแต่ตัวแรก
re.search ตรวจสอบตำแหน่งไหนของข้อความก้ได้
ตัวอย่างรูปแบบการใช้งาน ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=Uc_cplhRjDE&t=25s
ข้อแตกต่างระหว่างฟังก์ชัน re.match (รี.แมท) และ re.search (รีเสริด)
re.match เริ่มตรวจสอบรูปแบบของข้อความตั้งแต่ตัวแรก
re.search ตรวจสอบตำแหน่งไหนของข้อความก้ได้
ตัวอย่างรูปแบบการใช้งาน ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=Uc_cplhRjDE&t=25s
Last edited by dawthana on 23/01/2017 4:59 pm, edited 1 time in total.
-
- PHP Super Hero Member
- Posts: 604
- Joined: 07/12/2016 10:55 am
-
- PHP Super Hero Member
- Posts: 813
- Joined: 08/01/2018 9:55 am
Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)
Code: Select all
import re
a = 'Python 3.6.4'
match = re.search('(\d\.\d\.\d)', a)
if match:
print('version', match.group(1))
else:
print("did not")
I am slow walker, but I never walk back. (Abraham Lincoln)
- Jom07
- PHP Super Hero Member
- Posts: 514
- Joined: 08/01/2018 9:56 am
Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)
Code: Select all
import re
x = '<img src="image.jpg" vidth="100" />'
match = re.search('src="(.*)" vidth="(\d+)"', x)
if match:
print('src:', match.group(1))
print('vidth', match.group(2))
else:
print('not find')
ศึกษาข้อมูลจาก :https://www.youtube.com/watch?v=Uc_cplh ... M&index=49

-
- PHP Hero Member
- Posts: 199
- Joined: 30/04/2018 9:44 am
Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)
The Search Function (เดอะ เสริช ฟังก์ชัน)
ผลการรัน
ศึกษาจาก : https://www.youtube.com/watch?v=Uc_cplh ... lzdKrpxsMM
Code: Select all
import re
s = '<phone link="Mingphp.com" width="0899998899"'
match = re.search('link="(.*)" width="(\d+)"',s) #\d+ = number 0-9 infinity quality
if match:
print 'link : ', match.group(1)
print 'phone : ', match.group(2)
else:
print 'did not find'
It’s never too late to start again.
- jirawoot
- PHP VIP Members
- Posts: 3130
- Joined: 17/06/2019 10:30 am
Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)
Code: Select all
import re
b='Python 3.6'
match= re.search('(\d\.\d)', b)
if match:
print ('version', match.group(1))
else:
print('did not find')
Code: Select all
import re
x = '<img src="image.jpg" vidth="100" />'
match = re.search('src="(.*)" vidth="(\d+)"', x)
if match:
print('src:', match.group(1))
print('vidth', match.group(2))
else:
print('not find')
-
- PHP VIP Members
- Posts: 2997
- Joined: 04/06/2020 10:05 am
Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)
Code: Select all
import re
x = '<img src="image.jpg" width="100" height="120" />'
match = re.search('src="(.*)" width="(\d+)" height="(\d+)"', x)
if match:
print('src:', match.group(1))
print('width', match.group(2))
print('height', match.group(3))
else:
print('not find')
-
- Similar Topics
- Replies
- Views
- Last post
-
-
ทำความรู้จักกับภาษา python (49) : Search and Replace (เสริช แอนด์ ริพเลซ)
by dawthana » 23/01/2017 4:54 pm » in Python Knowledge - 4 Replies
- 856 Views
-
Last post by rangsan
05/05/2018 6:09 pm
-
-
- 0 Replies
- 1920 Views
-
Last post by ธวัชชัย แสนหาญ
04/01/2019 10:15 am
-
-
สอนการสร้างฟังก์ชั่น Function (ฟังก์ชั่น) Python (ไพทอน)
by md040 » 30/12/2016 4:51 pm » in Python Knowledge - 1 Replies
- 384 Views
-
Last post by md040
30/12/2016 4:51 pm
-
-
-
ทำความรู้จักกับภาษา python (23) : การใช้ from...import (ฟอร์ม...อิมพอร์ต) ในภาษา python (ไพทอน)
by dawthana » 12/01/2017 3:24 pm » in Python Knowledge - 9 Replies
- 2581 Views
-
Last post by jirawoot
21/06/2019 10:07 am
-
-
- 8 Replies
- 2432 Views
-
Last post by bolue
09/06/2020 10:22 am
-
-
ทำความรู้จักกับภาษา python (22) : การสร้าง Module (โมดูล) ใน python (ไพทอน)
by dawthana » 12/01/2017 11:33 am » in Python Knowledge - 9 Replies
- 1916 Views
-
Last post by jirawoot
20/06/2019 6:44 pm
-
-
-
ทำความรู้จักกับภาษา python (4) : ตัวดำเนินการใน python (ไพทอน)
by dawthana » 28/12/2016 10:05 am » in Python Knowledge - 11 Replies
- 1419 Views
-
Last post by Sirayu
19/06/2019 11:56 am
-
Who is online
Users browsing this forum: No registered users and 5 guests