ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

dawthana
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 604
ลงทะเบียนเมื่อ: 07/12/2016 10:55 am

ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)

โพสต์ที่ยังไม่ได้อ่าน โดย dawthana »

ฟังก์ชัน Search จะอยู่ในโมดูล re ใช้สำหรับค้นหาข้อความตามรูปแบบ (Patterns (แพทเทิล)) ที่กำหนด โดยจะคืนค่ากลับมาเป็น Object (อ๊อปเจค) ถ้าเจอข้อความ และคืนค่า None (นัน) ถ้าไม่เจอ
ข้อแตกต่างระหว่างฟังก์ชัน re.match (รี.แมท) และ re.search (รีเสริด)
re.match เริ่มตรวจสอบรูปแบบของข้อความตั้งแต่ตัวแรก
re.search ตรวจสอบตำแหน่งไหนของข้อความก้ได้
ตัวอย่างรูปแบบการใช้งาน
serach_1.png
serach_1.png (64.07 KiB) Viewed 2759 times
ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=Uc_cplhRjDE&t=25s
แก้ไขล่าสุดโดย dawthana เมื่อ 23/01/2017 4:59 pm, แก้ไขไปแล้ว 1 ครั้ง.
Four
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 813
ลงทะเบียนเมื่อ: 08/01/2018 9:55 am

Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)

โพสต์ที่ยังไม่ได้อ่าน โดย Four »

โค้ด: เลือกทั้งหมด

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
PHP Super Hero Member
โพสต์: 514
ลงทะเบียนเมื่อ: 08/01/2018 9:56 am

Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)

โพสต์ที่ยังไม่ได้อ่าน โดย Jom07 »

โค้ด: เลือกทั้งหมด

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
รูปภาพ
rangsan
PHP Hero Member
PHP Hero Member
โพสต์: 199
ลงทะเบียนเมื่อ: 30/04/2018 9:44 am

Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)

โพสต์ที่ยังไม่ได้อ่าน โดย rangsan »

The Search Function (เดอะ เสริช ฟังก์ชัน)

โค้ด: เลือกทั้งหมด

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'
ผลการรัน
output_search.png
output_search.png (3.81 KiB) Viewed 2647 times
ศึกษาจาก : https://www.youtube.com/watch?v=Uc_cplh ... lzdKrpxsMM
It’s never too late to start again.
ภาพประจำตัวสมาชิก
jirawoot
PHP VIP Members
PHP VIP Members
โพสต์: 3129
ลงทะเบียนเมื่อ: 17/06/2019 10:30 am

Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)

โพสต์ที่ยังไม่ได้อ่าน โดย jirawoot »

โค้ด: เลือกทั้งหมด

import re

b='Python 3.6'
match= re.search('(\d\.\d)', b)

if match:
    print ('version', match.group(1))
else:
    print('did not find')
Selection_003.png
Selection_003.png (6.2 KiB) Viewed 2215 times

โค้ด: เลือกทั้งหมด

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')
Selection_004.png
Selection_004.png (7.1 KiB) Viewed 2215 times
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25351
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ทำความรู้จักกับภาษา python (48) : The Search Function (เดอะ เสริช ฟังก์ชัน)

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

โค้ด: เลือกทั้งหมด

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')
ผลลัพธ์
Python Knowledge-1.png
Python Knowledge-1.png (4.39 KiB) Viewed 1942 times
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 38