Using int variable in regular expression

Discussion in 'Smartphones & Other Devices' started by jainsaniya, Aug 27, 2020.

  1. jainsaniya

    jainsaniya
    uix_expand uix_collapse
    New Member

    Joined:
    Aug 26, 2020
    Messages:
    1
    Likes Received:
    0
    I'm passing an int variable that tells me up to how many decimal points I should round my number. (e.g if the variable is 2 the number is rounded up to 2 places after the decimal point)

    After looking at other similar questions on Stack Overflow, I tried to do the following but it doesn't seem to be working. Round_num is the int variable

    number = re.compile(r'^[0-9]+([.][0-9]{1,'+ re.escape(round_num) +'})?$')
    Not using the variable as follows work though

    number = re.compile(r'^[0-9]+([.][0-9]{1,3})?$')
    How do I include an int variable into an regex? (The whole program is in <deleted> btw)
     
  2. fisicx

    fisicx
    uix_expand uix_collapse
    Active Member
    Community Liaison 1.0

    Joined:
    Mar 3, 2016
    Messages:
    2,582
    Likes Received:
    213
    As you had included a link to a python training course I suspect you don't really want an answer. You just wanted the linkdrop.
     
  3. VITS USA

    VITS USA
    uix_expand uix_collapse
    Member

    Joined:
    Aug 20, 2018
    Messages:
    848
    Likes Received:
    62
    I agree with fisicx, This is not the right way of the promotion of your website.
     

Share This Page