Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

regexp wizardry needed

by Anonymous Monk
on Jan 25, 2013 at 16:57 UTC ( [id://1015371]=perlquestion: print w/replies, xml ) Need Help??

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

PERL version 5.10 ======================== $lines2 = " PERIOD 13 OCT + 06 OCT"; $lines2 =~ /\d\d \w\w\w/g; $lines2 =~ s/(\d\d) (\w\w\w)/"\u\L$2 " . ($1 + 0)/eg; $lines2 =~ /(\w\w\w \d\d) \s+ (\w\w\w \d\d)/; print "dates: $1 and $2 \n"; #this line only works with 5.14. #push (@date, map { s/(\d\d) (\w\w\w)/"\u\L$2 " . ($1 + 0)/er } $lines +[2] =~ /\d\d \w\w\w/g); push (@date, $1); push (@date, $2); $date[0] = $date[0]. ", ". $currentyear; $date[1] = $date[1]. ", ". $currentyear;

Output:

dates: 06 and OCT

I need it to be

dates: Oct 06 and Oct 13

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: regexp wizardry needed
by Kenosis (Priest) on Jan 25, 2013 at 17:11 UTC

    Perhaps the following will be helpful:

    use strict; use warnings; my $lines2 = " PERIOD 13 OCT 06 OCT"; my @date = map ucfirst lc, split ' ', $lines2; print "$date[2] $date[3] and $date[2] $date[1]";

    Output:

    Oct 06 and Oct 13

    Update: Removed an unnecessary map. Thanks, johngg!

      my @date = map ucfirst, map lc, split ' ', $lines2;

      I'm not sure why you are going with two maps, one would be sufficient.

      $ perl -E ' > $line = q{PERIOD 13 OCT 06 OCT}; > say for map ucfirst lc, split q{ }, $line;' Period 13 Oct 06 Oct $

      I hope this is of interest.

      Cheers,

      JohnGG

        I'm not sure why you are going with two maps...

        I didn't think of it, so this is certainly of interest. Greatly appreciate you sharing this, JohnGG!

Re: regexp wizardry needed
by LanX (Saint) on Jan 25, 2013 at 17:03 UTC
    Please use <c> and <p>-tags and phrase a question.

    You'll be surprised how fast you'll get useful answers! =)

    see Writeup Formatting Tips

    Cheers Rolf

Re: regexp wizardry needed
by Anonymous Monk on Jan 25, 2013 at 17:06 UTC

    I added the correct HTML tags to the post to make it easier to get help. I am using Perl version 5.10

    $lines2 = " PERIOD 13 OCT 06 OCT"; $lines2 =~ /\d\d \w\w\w/g; $lines2 =~ s/(\d\d) (\w\w\w)/"\u\L$2 " . ($1 + 0)/eg; $lines2 =~ /(\w\w\w \d\d) \s+ (\w\w\w \d\d)/; print "dates: $1 and $2 \n"; #this line only works with 5.14. #push (@date, map { s/(\d\d) (\w\w\w)/"\u\L$2 " . ($1 + 0)/er } $lines +2 =~ /\d\d \w\w\w/g); push (@date, $1); push (@date, $2); $date[0] = $date[0]. ", ". $currentyear; $date1 = $date1. ", ". $currentyear;

    Output: dates: 06 and OCT
    I need it to be dates: Oct 06 and Oct 13

      perlop says that /r is for non-destructive operation of a regular expression. You can simply replace that by introducing a temporary variable:

      my $result = $example =~ s/foo/bar/r;

      ... can be replaced by

      (my $result=$example) =~ s/foo/bar/;

      In your case, you will have to make sure your map block actually returns the intended value:

      ... map { (my $result=$_) =~ s/foo/bar/; $result } @elements
        /r is only availabe in 5.14 I am using 5.10.
      Still no clear question, only a bunch of weird code! :(

      That's what you want?

      DB<159> @date = (" PERIOD 13 OCT 06 OCT" =~ m/ PERIOD (\d\d) (\w\w\w +) (\d\d) (\w\w\w)/) => (13, "OCT", "06", "OCT")

      If you only need the first letter of the month to be capitalized, try only matching the first letter and the rest, and process them accordingly with lc() or '\U' and '\L' in s///ubstitutions.

      UPDATE
      another way to do it, see ucfirst() (a command I've never used before =)...
      DB<164> ucfirst lc $date[1] => "Oct"

      Cheers Rolf

Re: regexp wizardry needed
by AnomalousMonk (Archbishop) on Jan 26, 2013 at 05:31 UTC

    Tested under 5.8 through 5.14:

    >perl -wMstrict -le "my $currentyear = '2013'; my $lines2 = ' PERIOD 13 OCT 06 OCT'; ;; $lines2 =~ s{ (\d\d) \s ([[:upper:]]{3} (?! \S)) } {\u\L$2 $1, $currentyear}xmsg; print qq{$lines2}; " PERIOD Oct 13, 2013 Oct 06, 2013

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found