How to normalize '1:2:3' to '01:02:03' by using re module of Python?

I want to normalize strings like




by using re module of Python, so I am trying to select the string like '1:2:3' then match the single number '1','2','3'..., here is my pattern:


it works but I think the pattern is not simple enough, anybody could help me simplify it? or use map()/split() if it's more sophisticated.


\b matches between a word character and a non-word character.

>>> import re
>>> l = ['1:2:3','10:20:30']
>>> [re.sub(r'\b(\d)\b', r'0\1', i) for i in l]
['01:02:03', '10:20:30']



You can simplify it to this.See demo.

If the string is like


Then do

print ",".join([re.sub(r"(?<!\d)(\d)(?!\d)",r"0\1",i) for i in x.split(",")])

You could do this with re, but pretty much nobody will know how it works afterwards. I'd recommend this instead:

':'.join("%02d" % int(x) for x in original_string.split(':'))

It's more clear how it works.

Category:python Time:2018-08-06 Views:0
Tags: regex python

Related post

  • How to disable individual tests temporarily using unittest module in python? 2010-01-14

    How to disable individual tests temporarily using unittest module in python? Like googletest does. Thanks. --------------Solutions------------- The latest version (2.7 - unreleased) supports test skipping/disabling like so. You could just get this mo

  • How to open a ssh connection using pxssh module in python 2015-01-11

    i want to open a ssh connection from one server to another,so trying to use pxssh module for this. I am getting the following error while running the below code: My Code: import pxssh s = pxssh.pxssh() if not s.login ('host','user','password'): print

  • How do I set an cache time for objects in OSCache using spring-modules? 2009-02-05

    I have an application in need of some caching, and for some of the semi-static data, I want them to stay in the cache a maximum amount of time (for instance 10 minutes) before being refreshed. My system merely retrieves data, never updates it, so I h

  • How best to hold 1000 different data series using TimeSeries module in Python? 2009-12-12

    I want to create a massive TimeSeries object which will hold 1000 different financial markets data series, each storing 1500 daily-data points. I'm quite new to the TimeSeries module and am a little confused as to how I would best go about it. So a f

  • How to run .exe in background, created from perl script by using pp module of perl PAR 2011-01-26

    I am using PAR:Packager to create exe from perl. I wish to make .exe with hidden functionality especially for windows i.e. executing in background for those system where active perl is not installed. how to do this?? --------------Solutions----------

  • How to override NULL value from aggregate query using MySQLdb module in python? 2010-02-23

    I am selecting the maximum date value from a MySQL database table in python using the MySQLdb module. If the result comes back as null, I want to override it with a default value. I could do this in the MySQL using a sub-query, but would prefer to do

  • How to delete () using re module in Python 2011-05-15

    I am in trouble for processing XML text. I want to delete () from my text as follows: from <b>(apa-bhari(n))</b> to <b>apa-bhari(n)</b> The following code was made name= re.sub('<b>\((.+)\)</b>','<b>\1</b

  • How do I integrate Pylint with Geany so that I can use Geany as a python IDE? 2011-05-26 This blog says to set build command as pylint -r no "%f" and set a custom error regex (W|E|F):([0-9]+):(.*) The commenter suggests that with command PYTHONPATH=${PYTHONPATH}:$(dirname %

  • How to normalize a signal to zero mean and unit variance? 2012-01-03

    I am new to MATLAB and I am trying to built a voice morphing system using MATLAB. So I would like to know how to normalize a signal to zero mean and unit variance using MATLAB? --------------Solutions------------- if your signal is in the matrix X, y

  • How to do drag & drop with wxWidgets module of Python? 2010-02-10

    I'm using Python and I want to do a drag & drop interface. For example, with a large picture whose size is bigger then the screen, I want to click on it and drag it to see other parts. Something like "google maps"! In google maps if we click two

  • How to normalize a path in PowerShell? 2009-01-30

    I have two paths: fred\frog and ..\frag I can join them together in PowerShell like this: join-path 'fred\frog' '..\frag' That gives me this: fred\frog\..\frag But I don't want that. I want a normalized path without the double dots, like this: fred\f

  • How to normalize a SQL Database 2011-02-27

    I was wondering if anyone had any suggestions on how to normalize a database. Now, I don't mean designing the structure, I mean how to actually move the database data from an old structure to the new, normalized structure. I know I could write someth

  • How to normalize a histogram in MATLAB 2011-03-16

    How to normalize a histogram, so it is a probability density (how is that the sum of all bins are equal to 1?). --------------Solutions------------- My answer to this is the same as in an answer to your earlier question. For a probability density fun

  • How Do You Communicate Service Layer Messages/Errors to Higher Layers Using MVP? 2008-08-22

    I'm currently writing an ASP.Net app from the UI down. I'm implementing an MVP architecture because I'm sick of Winforms and wanted something that had a better separation of concerns. So with MVP, the Presenter handles events raised by the View. Here

  • How can one host Flash content in a WPF application and use transparency? 2008-09-15

    How can I go about hosting flash content inside a WPF form and still use transparency/alpha on my WPF window? Hosting a WinForms flash controls does not allow this. --------------Solutions------------- Unless the control you use to display the Flash

  • How do I handle the window close event (user clicking the 'X' button) in a Python Tkinter program? 2008-09-21

    How do I handle the window close event (user clicking the 'X' button) in a Python Tkinter program? --------------Solutions------------- Use a Tkinter protocol handler to handle window manager events: root.protocol("WM_DELETE_WINDOW", handler) # or to

  • How to use Maven Modules without svn:externals 2008-10-03

    I have never quite understood how/why I would use Maven modules (reactor builds). We have tens of libraries that we share (as dependencies) among our products, and between libraries as well. If we were to switch to making them maven modules, how woul

  • How can I check if a box is alive or down using the ip address in Python? 2008-11-25

    The basic code is: from Tkinter import * import os,sys ana= Tk() def ping1(): os.system('ping') a=Button(pen) ip=("") a.config(text="PING",bg="white",fg="blue") a=ping1.ip ??? a.pack() ana.mainloop() How could I ping a sites or address? --

  • How can I check if an e-mail has been read using POP3/SMTP? 2008-12-22

    How can I check if an e-mail has been read using POP3/SMTP? I am able to read e-mails, but I can not figure out if the e-mail has been read or not. Any suggestions are appreciated. --------------Solutions------------- There is no completely reliable

Copyright (C), All Rights Reserved.

processed in 0.472 (s). 13 q(s)