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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

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

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

โดย MBMoo » 09/06/2020 11:38 am

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

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 1959 times

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

โดย jirawoot » 24/06/2019 11:52 am

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

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 2232 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 2232 times

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

โดย rangsan » 05/05/2018 5:51 pm

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 2664 times
ศึกษาจาก : https://www.youtube.com/watch?v=Uc_cplh ... lzdKrpxsMM

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

โดย Jom07 » 25/01/2018 6:25 pm

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

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

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

โดย Four » 25/01/2018 5:51 pm

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

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")
ผลรัน

รูปภาพ

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

โดย dawthana » 23/01/2017 4:27 pm

สามารถศึกษาเพิ่มเติมได้ที่ https://www.mindphp.com/vdo-tutorial-pyt ... ction.html

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

โดย dawthana » 23/01/2017 4:25 pm

ฟังก์ชัน Search จะอยู่ในโมดูล re ใช้สำหรับค้นหาข้อความตามรูปแบบ (Patterns (แพทเทิล)) ที่กำหนด โดยจะคืนค่ากลับมาเป็น Object (อ๊อปเจค) ถ้าเจอข้อความ และคืนค่า None (นัน) ถ้าไม่เจอ
ข้อแตกต่างระหว่างฟังก์ชัน re.match (รี.แมท) และ re.search (รีเสริด)
re.match เริ่มตรวจสอบรูปแบบของข้อความตั้งแต่ตัวแรก
re.search ตรวจสอบตำแหน่งไหนของข้อความก้ได้
ตัวอย่างรูปแบบการใช้งาน
serach_1.png
serach_1.png (64.07 KiB) Viewed 2776 times
ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=Uc_cplhRjDE&t=25s

ข้างบน