Using the re module to grab numbers in a text and make different tests with few code

Grab numbers with re

Let’s import re

import re

This is the module to search words, numbers and symbols in Python with regular expressions (that’s why the name of the module ‘re’).

Regular expression is a text made of letters, numbers and symbols that follow a certain patterns and that we want to find in a string.

A practical example of use of regular expression

We will show now the code to take all the numbers in a sentence to change them to make ten exercizes about proportions, os that we do not have to make each one of them, but just the first, as the other are make changing randomly the numbers.

The sentence is:

If we use 30 g of salt for 10 people, how many g we will use for 450 people?

# tests

import re

sentence = "If we use 30 g of salt for 10 people, how many g we will use for 450 people?"

numbers = re.findall("[0-9]+", sentence)

print(*numbers)

We searched all the numbers with “[0-9]+” that means that the pc search for numers from 0 to 9 and the + sign means that the number can have as many digit as possible. In the sentence there are 2 and 3 digit numbers. The output of the code is the following one:

30 10 450

We will now change randomly these numbers with randrange from the module random and with the method replace or the string objects.

import re
from random import randrange

sentence = "If we use 30 g of salt for 10 people, how many g we will use for 450 people?"

numbers = re.findall("[0-9]+", sentence)

print(*numbers, sep="\n")


def create_sentence():
    global numbers
    sentence2 = sentence[:]
    # cycle of the 3 numbers (in this case, but the numbers can be as many as there are in the sentence)
    new_numbers_list = []
    for each_number in numbers:
        each_number = int(each_number)

        #............========  CREATION OF RANDOM NUMBERS =======...........#

        # The random number will be chosen from the rante start to stop
        start = each_number / 2  # the smallest number
        stop = each_number * 3  # the biggest number
        step = int(each_number / 10 * 2)  # the step is the 20% of the original number
        # store the new numbers here to make the solution belowe
        new = int(randrange(start, stop, step))
        new_numbers_list.append(new)
        sentence2 = sentence2.replace(str(each_number), str(new))
        # at the end or the sentence we attach also the solution
    e1 = new_numbers_list[0]  # salt 1
    m1 = new_numbers_list[1]  # people 1
    m2 = None  # salt 2           x ?
    e2 = new_numbers_list[2]  # people 2 - how much salt for them?
    # e1 : m1 = x : e2 the solution e1 x e2 : m1
    # salt1 : people1 = salt2(x) _ people2
    sentence2 += f"Solution = {e1} x {e2} : {m1} = {round(e1*e2/m1,2)}"
    return sentence2


def print_excercizes(number=10):
    for n in range(number):
        print(create_sentence())


print_excercizes()

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.