Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Regex Tagging (newbie)

by cajun (Chaplain)
on May 05, 2001 at 15:43 UTC ( #78211=perlquestion: print w/replies, xml ) Need Help??

cajun has asked for the wisdom of the Perl Monks concerning the following question:

This should be a simple task, but I've not had any luck getting it to work.

I wish to change the string:

Sat May 5 02:29:11 2001

Into

May 5 02:29:11

Regex is certainly not a stong point of mine and I've never attempted to use tagging before.
my $now = time; $log_start=scalar localtime $now -1 * 86400; print "Log start = $log_start\n"; $log_start=~ /\w (\w\s+\d+\s\w)\s\d+/i; print "Modified Log start = $log_start\n";

This yeilds:

Use of uninitialized value in concatenation (.) at.....

Where am I going wrong here ?

Replies are listed 'Best First'.
(ar0n) Re: Regex Tagging (newbie)
by ar0n (Priest) on May 05, 2001 at 15:52 UTC
    I'm not sure what you mean by 'tagging', but here goes it:
    $log_start =~ s/^\w+\s+(\w+\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2})\s+\d{ +4}$/$1/i; print $log_start, "\n";
    The reason it wasn't working, was because your \w will only match 1 character, and no more. Not only that, you try to match the date which - if it were successful - it would save into $1, which you don't seem to be assigning to anything.

    I don't think
    Use of uninitialized value in concatenation (.) at.....
    has anything to do with the code you posted above, since there no actual concatenations taking place. At least, running the snippet with -w doesn't yield me any warnings.

    update modified regex slightly (added +'s). it works now.

    ar0n ]

      Thanks ar0n. I see your point about the \w.

      I've also tried printing $1.

      So the regex isn't matching. I replaced mine with yours.

      my $now = time; $log_start=scalar localtime $now -1 * 86400; print "Log start = $log_start\n"; $log_start=~ s/^\w+\s+(\w+\s\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})\s\d{4}$/ +$1/i; print "Modified Log start = $1\n";
      update

      Thanks to ar0n in CB, the correct regex is:

      $log_start=~ s/^\w+\s+(\w+\s+\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})\s\d{4}$ +/$1/i;
Re: Regex Tagging (newbie)
by mincus (Chaplain) on May 05, 2001 at 16:15 UTC
    If the day that you wanna remove is alway first, you could also use split.

    $log_start="Sat May 5 02:29:11 2001"; ($throw_away, @temp)=split(/ /,$log_start); $log_start=join(" ",@temp); print $log_start.$/;
    best of luck!


    .mincus
    telnet://bbs.mincus.com

Re: Regex Tagging (newbie)
by Daddio (Chaplain) on May 05, 2001 at 16:45 UTC

    You can also make this a whole lot simpler, since that date string is a constant format.

    $date =~ s/^\w+\s+(.*?)\s+\d+$/$1/;

    Same concept, just a little shorter!

    --
    D a d d i o
(crazyinsomniac) Re: Regex Tagging (newbie)
by crazyinsomniac (Prior) on May 06, 2001 at 04:32 UTC
    Fixed-format string manipulation using a regex is overkill.
    (unless you just wanna exercise)
    #!/usr/bin/perl -w use strict; my $str = 'Sat May 5 02:29:11 2001'; substr($str,0,4,''); substr($str,-4,4,''); print $str;
    update
    #This will also do in case you don't have 4 param substr (i forgot abo +ut that) $str = 'Sat May 5 02:29:11 2001'; substr($str,0,4) = ''; substr($str,-4,4) = '';

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://78211]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-10-27 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (91 votes). Check out past polls.

    Notices?