Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

regular expression question, need expert help

Hello,  i need a regular expression expert help on the following, have tried many combinations none seem to work.  

I have a string in the format "blabla anythinggoes Jun09B blabla"
need 2 regular expressions that read this string and returns

1) 06  (logic: find Jun then convert it to 06, need all 12 months, ie Jan becomes 01, Feb becomes 02 etc..)

2) 09 (logic: just return the 09 in Jun09B,  if it's Jun10D, it will return 10, if it's May08Z, it will return 08, etc..just return the 2 digit)

thank you
0
gagaliya
Asked:
gagaliya
  • 2
  • 2
  • 2
  • +1
5 Solutions
 
lwadwellCommented:
Hi gagaliya,

The conversion can be done using a look-up hash after the value is extracted.  Is the below something like what you want?


lwadwell
my %months = ( 'Jan' => '01',
               'Feb' => '02',
               'Mar' => '03',
               'Apr' => '04',
               'May' => '05',
               'Jun' => '06',
               'Jul' => '07',
               'Aug' => '08',
               'Sep' => '09',
               'oct' => '10',
               'Nov' => '11',
               'Dec' => '12'
             ); 
my $test_str = "blabla anythinggoes Jun09B blabla"; 
# Just the month
if ( $test_str =~ /(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{2}/ ) {
    printf "Month is %s or %s\n", $1, $months{$1};
} 
# Just the year
if ( $test_str =~ /[Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec](\d{2})/ ) {
    printf "Year is %s\n", $1;
} 
# Both at once
if ( $test_str =~ /(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(\d{2})/ ) {
    printf "Month is %s or %s in the year %s\n", $1, $months{$1}, $2;
}

Open in new window

0
 
Adam314Commented:
You don't need to specify all the months over again in the RE, you can get them from the keys of the %months.  Here is a simplified version.
my %months = ( 'Jan' => '01',
               'Feb' => '02',
               'Mar' => '03',
               'Apr' => '04',
               'May' => '05',
               'Jun' => '06',
               'Jul' => '07',
               'Aug' => '08',
               'Sep' => '09',
               'oct' => '10',
               'Nov' => '11',
               'Dec' => '12'
             ); 
my $months_re = join("|", keys %months);
 
my $test_str = "blabla anythinggoes Jun09B blabla"; 
 
# Just the month
if ( $test_str =~ /($months_re)/ ) {
    printf "Month is %s or %s\n", $1, $months{$1};
} 
 
# Just the year
if ( $test_str =~ /(\d{2})/ ) {
    printf "Year is %s\n", $1;
} 
# Both at once
if ( $test_str =~ /($months_re)(\d{2})/ ) {
    printf "Month is %s or %s in the year %s\n", $1, $months{$1}, $2;
}

Open in new window

0
 
ozoCommented:
Another simplification
@months{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)} = '01'..'12';
You may also want
$months_re = qr/\b(@{[join("|", keys %months]}/;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
gagaliyaAuthor Commented:
Sorry guys if i wasnt clear,  i am not looking for perl code, just pure regular expression. It's actually is used in a custom engine, it takes the string and the regular expression as 2 inputs, use it to evaluate the string, and spits out the output.
0
 
Adam314Commented:
The regular expression on it's own will not convert the month from a string to a number, unless you use multiple RE's, one for each month.
0
 
ozoCommented:
$_="blabla anythinggoes Jun09B blabla" ;
if( /\b(Jan|(Feb|(Mar|(Apr|(May|(Jun|(Jul|(Aug|(Sep|(Oct|(Nov|(Dec))))))))))))(\d\d)?/ ){
  $month=!!$1+!!$2+!!$3+!!$4+!!$5+!!$6+!!$7+!!$8+!!$9+!!$10+!!$11+!!$12;
  $year=$13;
}
0
 
gagaliyaAuthor Commented:
ok
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now