บทช่วยสอนงูหลาม

Python HOME แนะนำ Python Python เริ่มต้น ไวยากรณ์หลาม ความคิดเห็นหลาม ตัวแปรหลาม ประเภทข้อมูลหลาม ตัวเลขหลาม Python Casting Python Strings Python Booleans ตัวดำเนินการ Python รายการหลาม Python Tuples ชุดหลาม พจนานุกรม Python Python If...Else Python ในขณะที่ลูป Python สำหรับลูป ฟังก์ชั่นหลาม Python Lambda Python Arrays Python คลาส/วัตถุ มรดกหลาม Python Iterators ขอบเขตหลาม โมดูล Python วันที่หลาม คณิตศาสตร์หลาม Python JSON Python RegEx Python PIP Python ลอง...ยกเว้น อินพุตผู้ใช้ Python การจัดรูปแบบสตริงหลาม

การจัดการไฟล์

การจัดการไฟล์ Python Python อ่านไฟล์ Python เขียน/สร้างไฟล์ Python ลบไฟล์

โมดูล Python

กวดวิชา NumPy เกมส์หมีแพนด้า กวดวิชา Scipy

Python Matplotlib

บทนำ Matplotlib Matplotlib เริ่มต้น Matplotlib Pyplot Matplotlib พล็อต เครื่องหมาย Matplotlib Matplotlib Line ป้ายกำกับ Matplotlib Matplotlib Grid แผนย่อย Matplotlib Matplotlib Scatter Matplotlib บาร์ Matplotlib Histograms Matplotlib แผนภูมิวงกลม

การเรียนรู้ของเครื่อง

เริ่มต้น โหมดค่ามัธยฐาน ส่วนเบี่ยงเบนมาตรฐาน เปอร์เซ็นไทล์ การกระจายข้อมูล การกระจายข้อมูลปกติ พล็อตกระจาย การถดถอยเชิงเส้น การถดถอยพหุนาม การถดถอยพหุคูณ มาตราส่วน รถไฟ/ทดสอบ ต้นไม้การตัดสินใจ

Python MySQL

MySQL เริ่มต้น MySQL สร้างฐานข้อมูล MySQL สร้างตาราง MySQL Insert MySQL Select MySQL Where MySQL สั่งซื้อโดย MySQL Delete MySQL Drop Table MySQL Update MySQL Limit MySQL เข้าร่วม

Python MongoDB

MongoDB เริ่มต้น MongoDB สร้างฐานข้อมูล MongoDB สร้างคอลเล็กชัน MongoDB แทรก MongoDB ค้นหา แบบสอบถาม MongoDB MongoDB Sort MongoDB ลบ MongoDB Drop Collection อัพเดท MongoDB MongoDB Limit

การอ้างอิงหลาม

ภาพรวมของ Python ฟังก์ชันในตัวของ Python Python String Methods Python List Methods วิธีพจนานุกรม Python วิธี Python Tuple Python Set Methods วิธีไฟล์ Python คีย์เวิร์ด Python ข้อยกเว้นของ Python คำศัพท์หลาม

การอ้างอิงโมดูล

โมดูลสุ่ม โมดูลคำขอ โมดูลสถิติ โมดูลคณิตศาสตร์ โมดูล cMath

Python ฮาวทู

ลบรายการที่ซ้ำกัน ย้อนกลับสตริง เพิ่มสองตัวเลข

ตัวอย่าง Python

ตัวอย่าง Python Python Compiler แบบฝึกหัดหลาม แบบทดสอบ Python ใบรับรอง Python

Python RegEx


RegEx หรือ Regular Expression คือลำดับของอักขระที่สร้างรูปแบบการค้นหา

สามารถใช้ RegEx เพื่อตรวจสอบว่าสตริงมีรูปแบบการค้นหาที่ระบุหรือไม่


โมดูล RegEx

Python มีแพ็คเกจในตัวที่เรียกว่าreซึ่งสามารถใช้เพื่อทำงานกับนิพจน์ทั่วไป

นำเข้าreโมดูล:

import re

RegEx ใน Python

เมื่อคุณนำเข้าreโมดูลแล้ว คุณสามารถเริ่มใช้นิพจน์ทั่วไปได้:

ตัวอย่าง

ค้นหาสตริงเพื่อดูว่าเริ่มต้นด้วย "The" และลงท้ายด้วย "Spain" หรือไม่:

import re

txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)

ฟังก์ชัน RegEx

โมดูล นำreเสนอชุดของฟังก์ชันที่ช่วยให้เราค้นหาสตริงสำหรับการจับคู่:

Function Description
findall Returns a list containing all matches
search Returns a Match object if there is a match anywhere in the string
split Returns a list where the string has been split at each match
sub Replaces one or many matches with a string


เมตาคาแรคเตอร์

Metacharacters เป็นอักขระที่มีความหมายพิเศษ:

Character Description Example Try it
[] A set of characters "[a-m]"
\ Signals a special sequence (can also be used to escape special characters) "\d"
. Any character (except newline character) "he..o"
^ Starts with "^hello"
$ Ends with "planet$"
* Zero or more occurrences "he.*o"
+ One or more occurrences "he.+o"
? Zero or one occurrences "he.?o"
{} Exactly the specified number of occurrences "he{2}o"
| Either or "falls|stays"
() Capture and group    

ลำดับพิเศษ

ลำดับพิเศษ\ตามด้วยอักขระตัวหนึ่งในรายการด้านล่าง และมีความหมายพิเศษ:

Character Description Example Try it
\A Returns a match if the specified characters are at the beginning of the string "\AThe"
\b Returns a match where the specified characters are at the beginning or at the end of a word
(the "r" in the beginning is making sure that the string is being treated as a "raw string")
r"\bain"
r"ain\b"

\B Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word
(the "r" in the beginning is making sure that the string is being treated as a "raw string")
r"\Bain"
r"ain\B"

\d Returns a match where the string contains digits (numbers from 0-9) "\d"
\D Returns a match where the string DOES NOT contain digits "\D"
\s Returns a match where the string contains a white space character "\s"
\S Returns a match where the string DOES NOT contain a white space character "\S"
\w Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character) "\w"
\W Returns a match where the string DOES NOT contain any word characters "\W"
\Z Returns a match if the specified characters are at the end of the string "Spain\Z"

ชุด

ชุดคือชุดของอักขระภายในวงเล็บเหลี่ยม []ที่มีความหมายพิเศษ:

Set Description Try it
[arn] Returns a match where one of the specified characters (a, r, or n) are present
[a-n] Returns a match for any lower case character, alphabetically between a and n
[^arn] Returns a match for any character EXCEPT a, r, and n
[0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are present
[0-9] Returns a match for any digit between 0 and 9
[0-5][0-9] Returns a match for any two-digit numbers from 00 and 59
[a-zA-Z] Returns a match for any character alphabetically between a and z, lower case OR upper case
[+] In sets, +, *, ., |, (), $,{} has no special meaning, so [+] means: return a match for any + character in the string

 

ฟังก์ชัน findall()

ฟังก์ชันfindall()ส่งคืนรายการที่มีการแข่งขันทั้งหมด

ตัวอย่าง

พิมพ์รายการการแข่งขันทั้งหมด:

import re

txt = "The rain in Spain"
x = re.findall("ai", txt)
print(x)

รายการมีรายการที่ตรงกันในลำดับที่พบ

หากไม่พบรายการที่ตรงกัน รายการว่างจะถูกส่งคืน:

ตัวอย่าง

ส่งคืนรายการว่างหากไม่พบรายการที่ตรงกัน:

import re

txt = "The rain in Spain"
x = re.findall("Portugal", txt)
print(x)

 

ฟังก์ชั่นการค้นหา ()

ฟังก์ชันsearch()ค้นหาสตริงสำหรับการจับคู่ และส่งกลับออบเจกต์ Matchหากมีการจับคู่

หากมีการแข่งขันมากกว่าหนึ่งรายการ ระบบจะส่งคืนเฉพาะการแข่งขันครั้งแรกเท่านั้น:

ตัวอย่าง

ค้นหาอักขระช่องว่างสีขาวตัวแรกในสตริง:

import re

txt = "The rain in Spain"
x = re.search("\s", txt)

print("The first white-space character is located in position:", x.start())

หากไม่พบรายการที่ตรงกัน ค่าNoneจะถูกส่งคืน:

ตัวอย่าง

ทำการค้นหาที่ไม่ตรงกัน:

import re

txt = "The rain in Spain"
x = re.search("Portugal", txt)
print(x)

 

ฟังก์ชัน split()

ฟังก์ชันsplit()ส่งคืนรายการที่มีการแยกสตริงในแต่ละการแข่งขัน:

ตัวอย่าง

แยกที่อักขระช่องว่างสีขาวแต่ละตัว:

import re

txt = "The rain in Spain"
x = re.split("\s", txt)
print(x)

คุณสามารถควบคุมจำนวนครั้งโดยการระบุ maxsplit พารามิเตอร์:

ตัวอย่าง

แยกสตริงเฉพาะที่เกิดขึ้นครั้งแรก:

import re

txt = "The rain in Spain"
x = re.split("\s", txt, 1)
print(x)

 

ฟังก์ชันย่อย()

ฟังก์ชัน นี้sub()จะแทนที่การจับคู่ด้วยข้อความที่คุณเลือก:

ตัวอย่าง

แทนที่ทุกตัวอักษรเว้นวรรคด้วยตัวเลข 9:

import re

txt = "The rain in Spain"
x = re.sub("\s", "9", txt)
print(x)

คุณสามารถควบคุมจำนวนการแทนที่ได้โดยการระบุ count พารามิเตอร์:

ตัวอย่าง

แทนที่ 2 รายการแรก:

import re

txt = "The rain in Spain"
x = re.sub("\s", "9", txt, 2)
print(x)

 

จับคู่วัตถุ

Match Object เป็นวัตถุที่มีข้อมูลเกี่ยวกับการค้นหาและผลลัพธ์

หมายเหตุ:หากไม่มีการจับคู่ ค่าNoneจะถูกส่งคืน แทนที่จะเป็น Match Object

ตัวอย่าง

ทำการค้นหาที่จะส่งคืน Match Object:

import re

txt = "The rain in Spain"
x = re.search("ai", txt)
print(x) #this will print an object

Match object มีคุณสมบัติและวิธีการที่ใช้เพื่อดึงข้อมูลเกี่ยวกับการค้นหาและผลลัพธ์:

.span()ส่งคืนทูเพิลที่มีตำแหน่งเริ่มต้นและสิ้นสุดของการแข่งขัน
.stringส่งกลับสตริงที่ส่งผ่านไปยังฟังก์ชัน
.group()ส่งคืนส่วนของสตริงที่มีการจับคู่

ตัวอย่าง

พิมพ์ตำแหน่ง (ตำแหน่งเริ่มต้นและสิ้นสุด) ของการแข่งขันนัดแรก

นิพจน์ทั่วไปจะค้นหาคำใดๆ ที่ขึ้นต้นด้วยตัวพิมพ์ใหญ่ "S":

import re

txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())

ตัวอย่าง

พิมพ์สตริงที่ส่งผ่านไปยังฟังก์ชัน:

import re

txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)

ตัวอย่าง

พิมพ์ส่วนของสตริงที่มีการจับคู่

นิพจน์ทั่วไปจะค้นหาคำใดๆ ที่ขึ้นต้นด้วยตัวพิมพ์ใหญ่ "S":

import re

txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())

หมายเหตุ:หากไม่มีการจับคู่ ค่าNoneจะถูกส่งคืน แทนที่จะเป็น Match Object