Email Extraction and Random String Generation

  • Share this:

Code introduction


The function defines a regular expression pattern to match email addresses and then extracts all email addresses from the given text using this pattern. After that, a function named xxx is defined which accepts two parameters: one for the length of the string to be generated and another for the character set. The function first checks if the arguments are integers, then generates a random string and returns it.


Technology Stack : The function defines a regular expression pattern to match email addresses and then extracts all email addresses from the given text using this pattern. After that, a function named xxx is defined which accepts two parameters: one for the length of the string to be generated and another for the character set. The function first checks if the arguments are integers, then generates a random string and returns it.

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import re
import json
import sys
import time
import random

def extract_emails(text):
    """
    提取文本中的电子邮件地址
    """
    email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
    return re.findall(email_pattern, text)

def xxx(arg1, arg2):
    """
    生成随机字符串
    """
    if not isinstance(arg1, int) or not isinstance(arg2, int):
        raise ValueError("Both arguments must be integers")
    length = arg1
    characters = arg2
    if characters < 1 or length < 1:
        raise ValueError("Both arguments must be greater than 0")
    random_str = ''.join(random.choices('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=length))
    return random_str