Character Classes

เป็นตัวอักษร ระหว่างตัวนี้…ถึงตัวนี้

[abc]

ตรงกับ a หรือ b หรือ c เอามาตัวนึง

[aeiou]

ถ้าตรงกับ a,e,i,o,u หยิบตัวนั้นมา1ตัว

[a-c0–9]

ถ้าตรงกับa,b,c,0,1,2,3,4,5,6,7,8,9 หยิบมาตัวนึง

Photo by sydney Rae on Unsplash

Examples

ตัวอย่างเช่น

import re
text = 'word are weak ,week welk ,wek'
pattern = r'we[ae]k'
regex = re.compile(pattern)
word = regex.findall(text)
print(word)

[‘weak’, ‘week’]

มีตัวอักษรขึ้นต้น we

มีตัวอักษรลงท้าย k

ตัวอักษรตรงกลาง 1 ตัวเป็น a หรือeก็ได้

text = 'rolls are ab101,bc102 cd103 ef104'
pattern = r'[a-z]{2}\d{3}'
regex = re.compile(pattern)
case = regex.findall(text)
print(case)

[‘ab101’, ‘bc102’, ‘cd103’, ‘ef104’]

ขึ้นต้นด้วย a-z ติดกัน2ตัว

ลงท้ายด้วยตัวเลขติดกัน2ตัว

text = 'this is {+10},[ 20,+30], 70,-40'
pattern = r'\+?[^-]\d{2}'
regex = re.compile(pattern)
num = regex.findall(text)
print(num)

[‘+10’, ‘ 20’, ‘+30’, ‘ 70’]

\+? มีเครื่องหมาย+ตัวนึง หรือจะไม่มีก็ได้

[^-] ห้ามเครื่องหมายลบ

\d{2} ตัวเลขเขียนติดกัน2ตัว

text = 'word are cat mat,bat,rat'
pattern = r'[cmbr]at'

[‘cat’, ‘mat’, ‘bat’, ‘rat’]

ขึ้นต้น1ตัวด้วย c,m,b,r

ลงท้ายat

text = 'email id are abc-c8@hotmail.com , qrs.110@gmail.com'
pattern = r'\S+@\S+'

[‘abc-c8@hotmail.com’, ‘qrs.110@gmail.com’]

\S ตัวอักษรทั้งหมดที่ไม่ใช่ [\n\t\r\f\v]

+ยาวกี่ตัวก็ได้

@ มีเครื่องหมาย @

text = 'names are Karlen K,Mark M'
pattern = r'[A-Z][a-z]+\s[A-Z]'

[‘Karlen K’, ‘Mark M’]

[A-Z]ขึ้นต้นด้วยตัวใหญ่ A-Z

[a-z]+ตามด้วย a-z กี่ตัวก็ได้

\s เว้นว่าง

text = 'number are 100,200,20,3000,400'
pattern = r'\d{3}'

[‘100’, ‘200’, ‘300’, ‘400’]

ตัวเลขติดกัน3ตัว

text = 'number are 100,200,20,3000,400'
pattern = r'\d{2,4}'

[‘100’, ‘200’, ‘20’, ‘3000’, ‘400’]

ตัวเลขติดกัน 2 ตัว หรือ 3ตัว หรือ 4 ตัว

text = 'number are 100,200,20,3000,400'
pattern = r'\d{3,}'

[‘100’, ‘200’, ‘3000’, ‘400’]

ตัวเลขติดกันมากกว่าเท่ากับ 3 ตัว

#maximum possible
text = 'pattern is abcabcabcabc'
pattern = r'a[a-z]+c'
regex = re.compile(pattern)
mo = regex.search(text)
print(mo.group())

abcabcabcabc

มีเครื่องหมายบวก

#minimum posible
text = 'pattern is abcabcabcabc'
pattern = r'a[a-z]+?c' # ? is different
regex = re.compile(pattern)
mo = regex.search(text)
print(mo.group())

abc

เครื่องหมาย ?

Back References

ซ้ำกับตัวเอง

text = 'number are 1116,1414,2020,4032,505050'
pattern = r'(\d{2})\1'
regex = re.compile(pattern)
numbers = regex.findall(text)
print(numbers)
numbers = regex.finditer(text)
for n in numbers:
print(n.group())

[‘14’, ‘20’, ‘50’]
1414
2020
5050

text = 'phone is 032345678'
pattern = r'(\d{3})(\d{6})' # (group1 \d{3})(group2 \d{6})
regex = re.compile(pattern)
text = regex.sub(r'\1-\2',text)# sub
print(text)

phone is 032–345678

sub คือการแทนที่ข้อความเดิมด้วยรูปแบบใหม่

Positive Look Ahead

text = 'kalyan_cs,Meghana_cs ,John,jack'
pattern = '(?i)[a-z]+(?= cs)'
regex = re.compile(pattern)
names = regex.findall(text)
print(names)

[‘kalyan’, ‘Meghana’]

เอาข้อความก่อนหน้ากลุ่มของ _cs

text = 'values are 12,13,14a,15b'
pattern = r'\d{2}(?![a-z])'
regex = re.compile(pattern)
values = regex.findall(text)
print(values)

[‘12’, ‘13’]

อันนี้ต้องไม่มีกลุ่ม [a-z] ลงท้ายนะ

text = 'values are cs1001,cs1002,cs1003,1990'
pattern = '(?<=cs)\d{4}'
regex = re.compile(pattern)
values = regex.findall(text)
print(values)

[‘1001’, ‘1002’, ‘1003’]

มี cs นำหน้า

text = 'values are cs1001,cs1002,cs1003,1989'
pattern = '(?<!cs)\d{4}'
regex = re.compile(pattern)
values = regex.findall(text)
print(values)

[‘1989’]

ไม่มี cs นำหน้า

URL

file = open('url.txt','r')
text = file.read()
# www\ [a-z0-9]+ \[a-z]+
pattern = r'(?<!https://)www\.[a-z0-9]+\.[a-z]+'
regex = re.compile(pattern)
urls = regex.findall(text)
for u in urls:
print(u)

www.google.com
www.facebook.com
www.amazon.in
www.pqrs.in

ไม่เอาที่มี https

pattern = r'(?:https://)?www\.[a-z0-9]+\.[a-z]+'

www.google.com
www.facebook.com
https://www.yahoomail.com
https://www.microsoft.com
https://www.abc123.in
www.amazon.in
www.pqrs.in

มีหรือไม่มี https ก็ได้

pattern = r'(?:https://)www\.[a-z0-9]+\.[a-z]+'

https://www.yahoomail.com
https://www.microsoft.com
https://www.abc123.in

เอาเฉพาะ https นำหน้า

เอาเฉพาะ โดเมน .com
pattern = r'(?:https://)?www\.[a-z0-9]+\.com'
เอา .in หรือ .com ก้ได้
pattern = r'(?:https://)?www\.[a-z0-9]+(?:\.in|\.com)'

Dates

d = day ,m = month ,y = year

text = "dates are 22-06-1989 , 21/04/1992 , 12.04.1963"
pattern = r'\b\d{2}-\d{2}-\d{4}\b'
regex = re.compile(pattern)
dates = regex.findall(text)
print(dates)

[‘22–06–1989’]

เอาแบบ dd-mm-yyyy

text = "dates are 22-06-1989 , 21/04/1992 , 12.04.1963"
pattern = r'\b\d{2}[-/.]\d{2}[-/.]\d{4}\b'
regex = re.compile(pattern)
dates = regex.findall(text)
print(dates)

[‘22–06–1989’, ‘21/04/1992’, ‘12.04.1963’]

แบบ dd-mm-yyyy , dd/mm/yyyy , dd.mm.yyyy

text = "dates are 22-06-89 , 22-11-1985 , 21/04/1992 , 12.04.1963"
pattern = r'\b\d{2}-\d{2}-\d{2}(?:\d{2})?\b'
regex = re.compile(pattern)
dates = regex.findall(text)
print(dates)

[‘22–06–89’, ‘22–11–1985’]

เอาแบบ dd-mm-yy , dd-mm-yyyy

text = "dates are 22-06-89 , 22-11-1985 , 21/04/1992 , 12.04.1963"
pattern = r'\b\d{2}[-/.]\d{2}[-/.]1992\b'
regex = re.compile(pattern)
dates = regex.findall(text)
print(dates)

[‘21/04/1992’]

text = "dates are 22-06-89 , 22-11-1985 , 21/04/1992 , 12.04.1963"
pattern = r'\b\d{2}[-/.]\d{2}[-/.]19[89][0-9]\b'
regex = re.compile(pattern)
dates = regex.findall(text)
print(dates)

[‘22–11–1985’, ‘21/04/1992’]

เอาฌฉพาะปี 1980-1999

text = 'Dates are 22-01-1980,12-12-1984,12-05-1999,12-04-1963'
pattern= r'\b\d{2}-(?:0[13578]|1[02])-\d{4}\b'
regex = re.compile(pattern)
dates = regex.findall(text)
print(dates)

[‘22–01–1980’, ‘12–12–1984’, ‘12–05–1999’]

เอาฌฉพาะเดือนที่มี 31 วัน

# 10-89
text = 'the values are 10,110,140,95,90,60,45,09'
pattern = r'\b[1-8][0-9]\b'
regex = re.compile(pattern)
numbers = regex.findall(text)
print(numbers)

[‘10’, ‘60’, ‘45’]

ค่าระหว่าง 10–89

text = 'the values are 8,2,18,20,104,88,91,4003,26544'
pattern = r'\b(?:\d{2})+\b'
regex = re.compile(pattern)
numbers = regex.findall(text)
print(numbers)

[‘18’, ‘20’, ‘88’, ‘91’, ‘4003’]

เลือกตัวเลขที่มีจำนวน 2 ,4,6,8…. (\d{2})+

#100-150
text = 'the values are 8,2,18,20,104,88,91,4003,26544'
pattern = r'\b(1[0-4][0-9]|150)\b'
regex = re.compile(pattern)
numbers = regex.findall(text)
print(numbers)

[‘104’]

ค่าระหว่าง 100–150

text = 'emaill address is a123@hotmail.com , ass12@gmail.co.th'
pattern = r'\b[a-zA-Z0-9]+@[a-zA-Z0-9]+(?:\.com|\.co.th)\b'
regex = re.compile(pattern)
numbers = regex.findall(text)
print(numbers)

[‘a123@hotmail.com’, ‘ass12@gmail.co.th’]

Password check

  • length 8–32 characters
  • one upper case letter A-Z
  • one lower case letter a-z
  • one special character *,#,@,!,&
  • one digit 0–9
pwd = input('Enter the password :')
pattern1 = r'^.{8,32}$'
pattern2 = r'[^A-Za-z0-9#@*!&]'
pattern3 = r'^.*[A-Z].*$'
pattern4 = r'^.*[a-z].*$'
pattern5 = r'^.*[0-9].*$'
pattern6 = r'^.*[#@*!&].*$'
pattern7 = r'^.*(.)\1{2,}.*$'

if not re.search(pattern1,pwd):
print("Length isn't 8-32 characters")
elif re.search(pattern2,pwd):
print("password contains an invalid character")
elif not re.search(pattern3,pwd):
print("one Uppercase Letter A-Z")
elif not re.search(pattern4,pwd):
print("one Lowercase Letter a-z")
elif not re.search(pattern5,pwd):
print("one digit 0-9")
elif not re.search(pattern6,pwd):
print("one special character #@*!&")
elif re.search(pattern7,pwd):
print('password should not contain sequential identical')
else:
print(pwd+'is a valid password')

ก็จบละครับ สำหรับตัวอย่างในหลายๆแบบ หวังว่าจะพอมองรูปแบบการใช้งานของมันออก และนำไปประยุกต์ใช้กันได้นะครับ

--

--