Re: Parsing dates in files - must need to use SHIFT?



david.hunter@xxxxxxxxx wrote:
> Hi group - I need to retain the date in a file, and replace the forward
> slash date format with dashes - sounds simple!
>
> here's what I have:
> the current date format is --> 2005/07/31
> i need to change to --> 2005-07-31
>
> I need a recursive search and replace, but I'll worry aobut being
> recursive later - right now I can't seem to get my regex correct.

I'm not at all convinced that word means what you think it means.

> i can do a find on the date format like this:
> my $find = '\\d\d\d\d\/\d{2}\/\d{2}';
> my $replace = "-"
>
> <snip>
> $infile =~ s/${find}/${replace}/g;
>
>
> BUT - of course there are two issuies here:
> 1. The escape character in the $find var makes the script cawk out - I
> could only get it to work if I used:
> my $find = '\\d\d\d\d';

I have no idea what you mean by this. What does "cawk out" mean? Did
it give you some error message? What was that error message. And you
only got it to work by using that as opposed to what?

> 2. The entire string is replaced - removed the date digits.

Of course it does. That's what you told it to do. You said "Search for
this big string of digits, dash, digits, dash, digits, and replace the
whole thing with a single dash."

Your problem is not well specified, which makes giving you a 'best'
answer difficult. Does your string contain only the date? In that
case, just replace all slashes with dashes:

$str =~ tr{/}{-};

Does your string contain additional data beyond just the date? In that
case, search for the correct pattern, and replace it with what you want
there in its place:

my $str = 'This is a string with the date 2005/09/08 in it';
$str =~ s[ (\d{4}) / (\d{2}) / (\d{2}) ]
[$1-$2-$3]gx;

( /x modifier used for readability )

Paul Lalli

.



Relevant Pages

  • Re: Text formatted cells displaying numbers in scientific format
    ... When it 'sees' a large number it uses scientific format since ... I accept that Excel can't handle numbers greater than 15 digits and your ... string was 16 digits or more to be converted to a scientific value. ... It just doesn't display correctly in the cell (except after entering ...
    (microsoft.public.excel)
  • Re: Text formatted cells displaying numbers in scientific format
    ... It is just Excel trying to be helpful. ... When it 'sees' a large number it uses scientific format since ... I accept that Excel can't handle numbers greater than 15 digits and your ... string was 16 digits or more to be converted to a scientific value. ...
    (microsoft.public.excel)
  • Re: I hate dates
    ... the month as either a number or text string. ... the function requires the year to be four digits long. ... It is not advisable to do this (use two-digit years) as each computer can ... can then format anyway you want or need to using the Format function. ...
    (comp.lang.basic.visual.misc)
  • Re: I hate dates
    ... month as either a number or text string. ... the function requires the year to be four digits long. ... It is not advisable to do this (use two-digit years) as each computer can ... can then format anyway you want or need to using the Format function. ...
    (comp.lang.basic.visual.misc)
  • Re: DateTime.ParseExact weird behavior
    ... more inteligent behavior from the parser in this particular case. ... This is a string that is impossible to parse ... format to parse it. ... and as it can be either one or two digits it will read two ...
    (microsoft.public.dotnet.framework)