Search code examples
pythonregexstringinsertseparator

Insert char to string to end of number


I have ugly string:

oldstr = "0.100% fDrg: 2%,dgdv: 29% fGd dg 0.2%, Ghh-sf 2.2 dbgh: NONE dfgdf6 gd 3 "

I need to insert char | after the last digit of number for next splitting by this inserted |. There is also value none, where is also added this separator:

0.100| fdrg: 2|,dgdv: 29| fgd dg 0.2|, ghh-sf 2.2|dbgh: none| dfgdf6|gd 3|

I try this, but no success:

print re.sub(r'(\d+[a-z %^.])', r'\1|', oldstr.lower())

0.|100%| fdrg: 2%|,dgdv: 29%| fgd dg 0.|2%|, ghh-sf 2.|2 |dbgh: none dfgdf6 |gd 3 |

Any help will be appreciated.


Solution

  • You can use

    (\bnone\b|\d+(?:\.\d+)?)%?
    

    And replace with \1|.

    Explanation:

    • (\bnone\b|\d+(?:\.\d+)?) - Group 1 matching 2 alternatives:
      • \bnone\b - whole word none
      • | - or...
      • \d+(?:\.\d+)? - a float value (\d+ matches one or more digits, and (?:\.\d+)? matches (optionally) a dot followed with one or more digits)
    • %? - an optional (since ? means match one or zero times) % symbol

    See regex demo

    Python code:

    import re
    p = re.compile(ur'(\bnone\b|\d+(?:\.\d+)?)%?', re.IGNORECASE)
    test_str = "0.100% fDrg: 2%,dgdv: 29% fGd dg 0.2%, Ghh-sf 2.2 dbgh: NONE dfgdf6 gd 3 "
    subst = "\1|"
    result = re.sub(p, subst, test_str)
    

    If you need to trim the values, you will be able to do it after splitting. Also, none can be turned lower case before processing the text with re.sub(r'\b\none\b', 'NONE', input).