Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: srinivas devaki Newsgroups: comp.lang.python Subject: Re: There has to be a better way to split this string! Date: Wed, 10 Feb 2016 07:23:21 +0530 Lines: 48 Message-ID: References: <56BA91B5.5090400@cajuntechie.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de tU6OM603dyvis+cvi0e2GQbp2jwBxKhnP9F6NIeQC1bg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cc:addr:python-list': 0.09; 'filename.': 0.09; 'object;': 0.09; 'pgp': 0.09; 'subject:string': 0.09; 'url:github': 0.09; '+91': 0.15; 'everyone,': 0.15; 'message-----': 0.15; "skip:' 30": 0.15; '----- begin': 0.16; '2016': 0.16; '6:56': 0.16; 'hash:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sfxlen:0': 0.16; 'subject:There': 0.16; 'wrote:': 0.16; '>': 0.18; 'library': 0.20; 'student': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'junior': 0.22; 'parse': 0.22; 'am,': 0.23; 'feb': 0.23; 'header:In-Reply-To:1': 0.24; 'signed': 0.24; 'external': 0.27; 'skip:m 30': 0.27; 'message-id:@mail.gmail.com': 0.27; 'module.': 0.27; 'file': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'indian': 0.36; 'skip:m 40': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'received:209.85.213': 0.37; 'version': 0.38; 'received:209': 0.38; 'why': 0.39; 'easily': 0.39; 'school': 0.62; '(3rd': 0.84; 'mat': 0.84; 'ph:': 0.84; 'subject:this': 0.85 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=3cSVJMvTZzTjeXA6TygRzCHyhRa9RoCTSQX1V776oH4=; b=0bHJm7mT7Ehe+g18V3OigHs+d+LD1TFT7QSGVJfjYiF7W/IovvpPscPwLvOiW+ARrb p65QeiYwICMnz7+hW5iisay/U6UH/MhZdwAPmgKFLvjXxJ6QtlEqwN34Mve/y1Bc4rl8 /eiQSfhggz/+3farxbSdMqzFds/TNWLupk8UeWeB+hhqmAU33XxZ2Ubvz2GeMEs/GksU lGEa+SOgGvCsSr0Ax67qP0F9pCb097edt44POoK4Ek/Jjh8dcWjNIr7XkbiybajOaFd3 b8jDclV+DvimFE6S7fdeE0C8VssL95pBJU/vLQyA/k0I5mIP9Ssj3lrsAt9/WdeZyz5L ycdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=3cSVJMvTZzTjeXA6TygRzCHyhRa9RoCTSQX1V776oH4=; b=lyTiTuISKb+3HjYWQgNbrvJ15O7ejaA33XJ9Mj9Rt7UjwZNxIfvpuE4h6oVSXmkL5a ynygU1lUo1o3kXvJlD08kQ3/FCP6Yt+8YEmDIgNtyLNBEQay3ZTAPtU1QKA8aV/wHdu5 fiJKWu/R1qr5P21/jiv/K9+k9+M+pz9ZqOpvByqyb+iPzHPMesYvPiEsHBw5yNrBhwlY cUaUSuLkdkXqtcwBUw5RaWZQVAZ5s/+7n8ysqj77eivAg8tTp9CHCERvvENeTLCVKlpO x94xMojSPe+c3OO6OQidn2E+9PCIqtqZst8RHzvXeqaIltpYULpRtMrjlRtbxZO6gKmm LNyA== X-Gm-Message-State: AG10YOQIUrJkwP5VAcLtEFP6QhMWT4bHlIpWFFH6XdhxnNhzVk4se34r4QVDObYceX+rJjfnZyKZT2Ghrppj7g== X-Received: by 10.50.8.102 with SMTP id q6mr4714211iga.44.1455069201847; Tue, 09 Feb 2016 17:53:21 -0800 (PST) In-Reply-To: <56BA91B5.5090400@cajuntechie.org> X-Content-Filtered-By: Mailman/MimeDel 2.1.21rc2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102748 On Feb 10, 2016 6:56 AM, "Anthony Papillion" wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA512 > > Hello Everyone, > > I am using datetime.now() to create a unique version of a filename. > When the final file is named, it will look something like: > > myfile-2015-02-09-19-08-45-4223 > You can easily do this(retrieving the tokens) using re module. In [33]: mat = re.search(r'(\S+)-(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{4})', 'myfi-le-2015-02-09-19-08-45-4223') In [34]: mat Out[34]: <_sre.SRE_Match object; span=(0, 32), match='myfi-le-2015-02-09-19-08-45-4223'> In [35]: mat.groups() Out[35]: ('myfi-le', '2015', '02', '09', '19', '08', '45', '4223') In [36]: mat = re.search(r'(\S+)-(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{4})', 'myfile-2015-02-09-19-08-45-4223') In [37]: mat Out[37]: <_sre.SRE_Match object; span=(0, 31), match='myfile-2015-02-09-19-08-45-4223'> In [38]: mat.groups() Out[38]: ('myfile', '2015', '02', '09', '19', '08', '45', '4223') if you don't want fiddle with regex you can use parse module( https://github.com/r1chardj0n3s/parse). but why use an external library when stdlib already provides it? :) Regards Srinivas Devaki Junior (3rd yr) student at Indian School of Mines,(IIT Dhanbad) Computer Science and Engineering Department ph: +91 9491 383 249 telegram_id: @eightnoteight