Regular expressions
Roy Smith
roy at panix.com
Mon Dec 26 19:07:49 EST 2011
In article
<495b6fe6-704a-42fc-b10b-484218ad8409 at b20g2000pro.googlegroups.com>,
"mauriceling at acm.org" <mauriceling at 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)
More information about the Python-list
mailing list