Extract Numbers from Text Function

  • Share this:

Code introduction


This function extracts all numbers from the given text and returns a list of integers.


Technology Stack : Regular expressions (re), list comprehension

Code Type : String processing

Code Difficulty : Intermediate


                
                    
import re
import math
import random
import datetime

def extract_numbers(text):
    # 使用正则表达式提取文本中的数字
    numbers = re.findall(r'\b\d+\b', text)
    return [int(num) for num in numbers]