roberthsu2003 / __2024_04_17_mon_wed__

Python與AI人工智慧開發入門
24 stars 5 forks source link

使用正規則表達式 #21

Open roberthsu2003 opened 4 months ago

roberthsu2003 commented 4 months ago
建立一個正規則表達式
有效的電話號碼:(123) 456-7890
無效的電話號碼:123-456-7890
有效日期:2024-05-29
無效日期:2024-13-01

pattern = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])'
請建立一個輸入的範例,判斷格式是否正確
chesterXalan commented 4 months ago
import re

phone_num = input('請輸入手機號碼(xxx) xxx-xxxx: ')
phoneNumRegex = re.compile(r'\(\d{3}\) \d{3}-\d{4}')
match1 = phoneNumRegex.match(phone_num)
if match1 is not None:
    print(f'{match1.group()}: 格式符合')
else:
    print('格式不符')

螢幕擷取畫面 2024-05-29 221429

date = input('請輸入日期yyyy-mm-dd: ')
dateRegex = re.compile(r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])')
match2 = dateRegex.match(date)
if match2 is not None:
    print(f'{match2.group()}: 格式符合')
else:
    print('格式不符')

螢幕擷取畫面 2024-05-29 221456

ccanna commented 4 months ago
import re

phone_num =input("請輸入有效日期(xxx) xxx-xxxx:")
phoneNumberRegex:re.Pattern = re.compile(r'\(\d{3}\) \d{3}-\d{4}')     
match:re.Match | None = re.match(phoneNumberRegex, phone_num)
if match is not None:
    print(f"有效的電話號碼:{match.group()}")
else:
    print(f"無效的電話號碼:{phone_num}")

date =input("請輸入有效日期xxxx-xx-xx:")
daterangeRegex:re.Pattern = re.compile(r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])')
match1:re.Match | None = re.match(daterangeRegex, date)
if match1 is not None:
    print(f"有效的日期:{match1.group()}")
else:
    print(f"無效日期:{date}")

image image

Tony840705 commented 4 months ago
import re

phone_num =input("請輸入電話號碼驗證:(xxx) xxx-xxxx:")
phoneNumberRegex:re.Pattern = re.compile(r'\(\d{3}\) \d{3}-\d{4}')     
match:re.Match | None = re.match(phoneNumberRegex, phone_num)
if match is not None:
    print(f"有效的電話號碼:{match.group()}")
else:
    print(f"無效的電話號碼:{phone_num}")

date =input("請輸入日期驗證:xxxx-xx-xx:")
daterangeRegex:re.Pattern = re.compile(r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])')
match1:re.Match | None = re.match(daterangeRegex, date)
if match1 is not None:
    print(f"有效的日期:{match1.group()}")
else:
    print(f"無效的日期:{date}")

0529HW1

0529HW2

PercJK commented 4 months ago
import re

phone_num = input('請輸入手機號碼(xxx) xxx-xxxx: ')
phoneNumRegex = re.compile(r'\(\d{3}\) \d{3}-\d{4}')
match1 = phoneNumRegex.match(phone_num)
if match1 is not None:
    print(f'{match1.group()}: 格式符合')
else:
    print('格式不符')

lesson13_1

import re
date = input('請輸入日期yyyy-mm-dd: ')
dateRegex = re.compile(r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])')
match1 = dateRegex.match(date)
if match1 is not None:
    print(f'{match1.group()}: 格式符合')
else:
    print('格式不符')

lesson13

chiayuben commented 4 months ago
import re
phone_num = input("請輸入手機號碼(xxxx)-xxx-xxx:")
phoneNumRegex:re.Pattern = re.compile(r'(\(\d{4}\))-(\d{3})-(\d{3})') 
match1:re.Match | None = re.match(phoneNumRegex,phone_num)
if match1 is not None:
    print(f"{match1.group()}有效的電話")

else:
    print(f"{phone_num}無效的電話")

date = input("請輸入日期xxxx-xx-xx:")
dateRegex:re.Pattern=re.compile(r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])')
match_date:re.match=re.match(dateRegex,date)
if match_date is not None:
    print(f'你輸入的日期為{match_date.group()}')
else:
    print(f"{date}為無效的日期,請重新輸入")

有效的電話、日期 image

無效的電話、日期 image

chihweihan commented 4 months ago
import re
phone_num = input("請輸入手機號碼:(xxx) xxx-xxxx")
phoneNumRegex:re.Pattern = re.compile(r'(\(\d{3}\)) \d{3}-\d{4}')
match1:re.Match | None = re.match(phoneNumRegex,phone_num)
if match1 is not None:
    print(f"有效的電話號碼:{match1.group()}") 

else:
    print(f"無效的電話號碼:{phone_num}")

date = input("請輸入有效日期:xxxx-xx-xx")
dateRegex:re.Pattern = re.compile(r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])')
match2:re.Match | None = re.match(dateRegex,date)
if match2 is not None:
    print(f"有效日期:{match2.group()}")  

else:
    print(f"無效日期:{date}")

06041 06042