Inserting '-' character in front of all numbers in a string

Jorgen Grahn grahn+nntp at snipabacken.dyndns.org
Tue Apr 3 09:21:12 EDT 2007


On 30 Mar 2007 08:38:27 -0700, kevinliu23 <kevinliu23 at gmail.com> wrote:
> Hey guys,
>
> I want to be able to insert a '-' character in front of all numeric
> values in a string. I want to insert the '-' character to use in
> conjunction with the getopt.getopt() function.
...
> "2a 3ab" into "-2a -3ab".

Are you sure you want getopt, or are you just reusing it because you
don't know enough about string parsing and REs?

-2a -3ab is a bit limited: if you run out of digits and have to use
10, 11, ... then getopt will treat '-10ab' as '-1' without argument
and -0 with 'ab' as argument. It will probably choke on the
argumentless -1, too.

/Jorgen

-- 
  // Jorgen Grahn <grahn@        Ph'nglui mglw'nafh Cthulhu
\X/     snipabacken.dyndns.org>  R'lyeh wgah'nagl fhtagn!



More information about the Python-list mailing list