簡體   English   中英

使用正則表達式識別 python 中的字符和數字

[英]using regex to identify characters and digits in python

我的電話號碼可能如下所示:

927-6847
611-6701p3715ou264-5435
869-6289fillemichelinemoisan
613-5000p4238soirou570-9639cel

等等...

我想識別並將它們分成:

9276847
6116701
2645435
8696289
6135000
5709639

存儲在其他地方的字符串:

611-6701p3715ou264-5435
869-6289fillemichelinemoisan
613-5000p4238soirou570-9639cel

當數字之間有一個p時,p 之后的數字是一個擴展 - 獲取 p 之前的數字並將整個字符串保存在其他地方 當有ou時,另一個數字在該數字之后開始 當有cel或任何隨機字符串時,獲取數字部分並將整個字符串保存在其他地方

編輯:這是我嘗試過的:

phNumber='928-4612cel'
if not re.match('^[\d]*$', phNumber):
     res = re.match("(.*?)[a-z]",re.sub('[^\d\w]', '', phNumber)).group(1)    

我正在尋找處理案例並確定哪些字符串在通過正則表達式被切斷之前具有更多字符

首先讓我再次確認您的要求:

  1. 找出模式為“xxx-xxxx”的數字,其中x是0-9中的任意數字,然后保存模式為“xxxxxxx”的數字。
  2. 如果文本中有任何隨機字符串,則保存整個字符串。
import re

# make a list to input all the string want to test, 
EXAMPLE = [
    "927-6847",
    "9276847"
    "927.6847"
    "611-6701p3715ou264-5435",
    "6116701p3715ou264-5435",
    "869-6289fillemichelinemoisan",
    "869.6289fillemichelinemoisan",
    "8696289fillemichelinemoisan",
    "613-5000p4238soirou570-9639cel",
]

def save_phone_number(test_string,output_file_name):
    number_to_save = []

    # regex pattern of "xxx-xxxx" where x is digits
    regex_pattern = r"[0-9]{3}-[0-9]{4}"
    phone_numbers = re.findall(regex_pattern,test_string)

    # remove the "-"
    for item in phone_numbers:
        number_to_save.append(item.replace("-",""))

    # save to file
    with open(output_file_name,"a") as file_object:
        for item in number_to_save:
            file_object.write(item+"\n")

def save_somewhere_else(test_string,output_file_name):
    string_to_save = []

    # regex pattern if there is any alphabet in the string
    # (.*) mean any character with any length
    # [a-zA-Z] mean if there is a character that is lower or upper alphabet
    regex_pattern = r"(.*)[a-zA-Z](.*)"
    if re.match(regex_pattern,test_string) is not None:
        with open(output_file_name,"a") as file_object:
            file_object.write(test_string+"\n")

if __name__ == "__main__":

    phone_number_file = "phone_number.txt"
    somewhere_file = "somewhere.txt"

    for each_string in EXAMPLE:
        save_phone_number(each_string,phone_number_file)
        save_somewhere_else(each_string,somewhere_file)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM