Splitting device addresses into parts

johnzenger at gmail.com johnzenger at gmail.com
Tue Sep 26 13:31:27 EDT 2006


This may be a rare case where regular expressions are not a horrible,
self-defeating idea.  Something like:

delimiter = re.compile("[:\.]")
delimiter.split("PCI:2:3.0")
...and then ignore the first entry, and map int the rest.
Alternatively, if the delimiters can really be anything, and if there
are no numbers in the first space ("PCI"), then maybe this approach:

number = re.compile("\d+?")
number.findall("PCI:2:3.0")

Fabian Steiner wrote:
> Bruno Desthuilliers wrote:
> > Fabian Steiner wrote:
> >> I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and
> >> need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any
> >> simple way to achieve this? So far I am using regular expressions but I
> >> would like to avoid them ...
> >
> > devices = ["PCI:2:3.0", "PCI:3.4:0"]
> > for d in device:
> >   nums = tuple(map(int, d.split(':')[1:]))
> >   print "for ", d, " : ", nums
>
> Unfortunately, this doesn't work (even if I correct your typos) since
> the delimeter isn't necessary a colon - that's exactly the difficulty I
> am trying to solve.
> 
> Regards,
> Fabian Steiner




More information about the Python-list mailing list