Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17995

Re: Regular expressions

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Regular expressions
Date 2011-12-26 19:07 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-1ED2CF.19074926122011@news.panix.com> (permalink)
References <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com>

Show all headers | View raw


In article 
<495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com>,
 "mauriceling@acm.org" <mauriceling@gmail.com> wrote:

> Hi
> 
> I am trying to change "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:
> 0:" to "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1".
> 
> Can anyone help me with the regular expressions needed?

Easy-peasy:

import re
input = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: 0:"
output = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1"
pattern = re.compile(
    r'@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: 0:')
out = pattern.sub(
    r'@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1',
    input)
assert out == output

To be honest, I wouldn't do this with a regex.  I'm not quite sure what 
you're trying to do, but I'm guessing it's something like "Get 
everything after the first space in the string; keep just the integer 
that's before the first ':' in that and turn the space into a slash".  
In that case, I'd do something like:

head, tail = input.split(' ', 1)
number, _ = tail.split(':')
print "%s/%s" % (head, number)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Regular expressions "mauriceling@acm.org" <mauriceling@gmail.com> - 2011-12-26 15:45 -0800
  Re: Regular expressions Chris Angelico <rosuav@gmail.com> - 2011-12-27 11:00 +1100
    Re: Regular expressions "mauriceling@acm.org" <mauriceling@gmail.com> - 2011-12-26 16:15 -0800
      Re: Regular expressions Fredrik Tolf <fredrik@dolda2000.com> - 2011-12-27 06:01 +0100
        Re: Regular expressions rusi <rustompmody@gmail.com> - 2011-12-27 23:05 -0800
  Re: Regular expressions Roy Smith <roy@panix.com> - 2011-12-26 19:07 -0500
  Re: Regular expressions Jason Friedman <jason@powerpull.net> - 2011-12-27 00:16 +0000
    Re: Regular expressions "mauriceling@acm.org" <mauriceling@gmail.com> - 2011-12-26 16:24 -0800
      Re: Regular expressions Jason Friedman <jason@powerpull.net> - 2011-12-27 01:26 +0000

csiph-web