Regular expressions

mauriceling@acm.org mauriceling at gmail.com
Mon Dec 26 19:24:35 EST 2011


On Dec 27, 8:16 am, Jason Friedman <ja... at powerpull.net> wrote:
> > On Tue, Dec 27, 2011 at 10:45 AM, mauricel... at acm.org
> > <mauricel... at gmail.com> wrote:
> >> Hi
>
> >> I am trying to change <one string> to <another string>.
>
> >> Can anyone help me with the regular expressions needed?
>
> > A regular expression defines a string based on rules. Without seeing a
> > lot more strings, we can't know what possibilities there are for each
> > part of the string. You probably know your data better than we ever
> > will, even eyeballing the entire set of strings; just write down, in
> > order, what the pieces ought to be - for instance, the first token
> > might be a literal @ sign, followed by three upper-case letters, then
> > a hyphen, then any number of alphanumerics followed by a colon, etc.
> > Once you have that, it's fairly straightforward to translate that into
> > regex syntax.
>
> > ChrisA
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> The OP told me, off list, that my guess was true:
>
> > Can we say that your string:
> > 1) Contains 7 colon-delimited fields, followed by
> > 2) whitespace, followed by
> > 3) 3 colon-delimited fields (A, B, C), followed by
> > 4) a colon?
> > The transformation needed is that the whitespace is replaced by a
> > slash, the "A" characters are taken as is, and the colons and fields
> > following the "A" characters are eliminated?
>
> Doubtful that my guess was 100% accurate, but nevertheless:
>
> >>> import re
> >>> string1 = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:0:"
> >>> re.sub(r"(\S+)\s+(\S+?):.+", "\g<1>/\g<2>", string1)
>
> '@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1'

Thanks a lot everyone.

Can anyone suggest a good place to learn REs?

ML



More information about the Python-list mailing list