Home > Mobile >  How to replace string before string using regex?
How to replace string before string using regex?

Time:03-22


def f(float):
    tempstr = 0
    if float <= 0.5:
        tempstr = 'range1'
    if 0.6 <= float <= 1:
        tempstr = 'range2'
    if 1.1 <= float <= 2:
        tempstr = 'range3'
    if 2.1 <= float <= 3:
        tempstr = 'range4'
    if 3.1 <= integer <= 4:
        tempstr = 'range5'
    return(tempstr)

I have a string, I have to replace float which comes before cm with "range-i". There can be any number of spaces between the float and cm.

Example:-

string = 'this is 0.2 cm and that is 3.4 cm'

new_string = this is range1 cm and that is range5 cm (only one white space in between float and cm)

I want an efficient and simple solution using regex.

CodePudding user response:

Don't shadow built-in functions, which means to call a variable with the same name of smt already defined, in this case float(), doc... call it float_ if you really lake of fantasy, ok?

More information on regex can be found here.

s = 'this is 0.2 cm and that is 3.4 cm and now a 1.2 cm'

def f(float_):
    f = float(float_.group(1)) # casting the match result to float for comparison

    tempstr = '0' # <-- a string not an integer!
    if f <= 0.5:
        tempstr = 'range1'
    if 0.6 <= f <= 1:
        tempstr = 'range2'
    if 1.1 <= f <= 2:
        tempstr = 'range3'
    if 2.1 <= f <= 3:
        tempstr = 'range4'
    if 3.1 <= int(f) <= 4:
        tempstr = 'range5'
    return tempstr


import re

print(re.sub(r'(\d\.\d*)', f, s))

Output

this is range1 cm and that is 0 cm and now a range3 cm

CodePudding user response:

If the range of float values in your script is assumed to be 0..4 then you just need to adjust a few things.

In the if statements, you are not checking the continuous ranges containing 0.5 to 0.6, 1 to 1.1, 2 to 2.1, and 3 to 3.1. Adjust each if statement to include the upper value of the previous range:

if 0.5 < float <= 1:

You also had a NameError on integer, so this name was changed to the same as the other if statements.

This code below will encompass all values between 0 and 4:

import re

def f(float_val):
    float_val = float(float_val.group(1))
    tempstr = 0
    if 0 <= float_val <= 0.5:
        tempstr = 'range1'
    if 0.5 < float_val <= 1:
        tempstr = 'range2'
    if 1 < float_val <= 2:
        tempstr = 'range3'
    if 2 < float_val <= 3:
        tempstr = 'range4'
    if 3 < float_val <= 4:
        tempstr = 'range5'
    return(tempstr)


s = 'this is 0.55 cm and that is 3.4 cm and now a 1.05 cm'
print(re.sub(r'(\d\.\d*)', f, s))

CodePudding user response:

You are almost there...

I would do:

def f(fl_):
    tempstr = '0'
    fl=float(fl_)
    if fl <= 0.5:
        tempstr = 'range1 '
    if 0.6 <= fl <= 1:
        tempstr = 'range2 '
    if 1.1 <= fl <= 2:
        tempstr = 'range3 '
    if 2.1 <= fl <= 3:
        tempstr = 'range4 '
    if 3.1 <= fl <= 4:
        tempstr = 'range5 '
    return(tempstr)

>>> string = 'this is 0.2 cm and that is 3.4 cm'
>>> re.sub(r'(\d \.\d )\s ', lambda m: f(m.group(1)), string)
'this is range1 cm and that is range5 cm'

Note: There are discontinuities in your ranges such as .55 and 1.05. Is this intensional?

  • Related