Re: Dummy regex question

From: Tad McClellan (tadmc_at_augustmail.com)
Date: 01/06/05


Date: Wed, 5 Jan 2005 18:21:24 -0600

Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@gmx.net> wrote:
> JayEs wrote:
>
>
>> Given a string that can be:
>> "$ 12.00"
>> "USD 187.54"
>> "CAD 1.20"
>> "??? 12.65"
>>
>> (the latter due to special characters not avail in ASCII)
>>
>> How do I split these strings into 2, separating the currency code and the
>> value?
>

> I don't get why everyone tries to use split() here.

Because it is the right tool for the job.

You use split() when you know what you want to discard.

You use m// in a list context when you know what you want to keep.

> I'd take a simple
> regexp looking like
>
> m/(.+)\s+(\d+\.\d+)/
> my ($currency, $amount) = ($1, $2);

You should never use the dollar-digit variables unless you have
first ensured that the pattern match *succeeded*.

   if ( m/(.+)\s+(\d+\.\d+)/ )
      { ($currency, $amount) = ($1, $2) }

or

   my ($currency, $amount) = m/(.+)\s+(\d+\.\d+)/; # m// in list context

-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


Relevant Pages

  • Re: Convert a string to Ascii codes and then back to string again
    ... Now, I don't know in which strings the characters will be, and and I don't ... Any special characters has to be sent ... function and return the "Ascii" codes so I can write it directly to the printerport. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: About Windows address space
    ... equally compare dword for dword (just being careful of strings not ... someone's "knee ache" in a medical examination while the patient is ... be faster than ASCII strings, anyway"...or, if you like, "ASCII ... ASCII method generally...think it's totally dumb that Windows uses it ...
    (alt.lang.asm)
  • Re: Special Characters (Unicode, Ascii) in Python and MySQL
    ... Special characters and punctuation all seem not to be stored and ... perhaps the encoding of the database itself should be different? ... 'ascii' codec can't decode byte 0xe2 in position ... and pasted from Microsoft Word. ...
    (comp.lang.python)
  • Re: any clever vectorising idea?
    ... even if strings consist of ASCII numbers. ... The interpretation of the control ... array processing. ...
    (comp.soft-sys.matlab)
  • Re: binary i/o files
    ... So I have suggested on how to write the full ascii 256-character set ... to unformatted sequential, unbridged BINARY stream files. ... you can write out some ascii strings directly if they do not ... your own preferred end-of-line character combination. ...
    (comp.lang.fortran)