schemanic
schemanic

Reputation: 99

Search a string for dates in perl based on strptime string in Perl?

I need a way to search any string that contains a valid whole or partial datetime and extract it from that string, based on a strptime format.

For example, if I have:

%b%d

How can I search this string:

"asdbsdjwebzadjabsdmwAug20asdfswelasdsvclasdk"

and get some datetime representation of "August 20th" from it?

Are there any modules that allow me to convert a strptime format into a regex and search for it like that?

Any information would be helpful. Thanks!

Upvotes: 0

Views: 114

Answers (1)

ThisSuitIsBlackNot
ThisSuitIsBlackNot

Reputation: 24083

You can use Regexp::Common::time:

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Regexp::Common qw(time);

my $str = "asdbsdjwebzadjabsdmwAug20asdfswelasdsvclasdk";
say "matches" if $str =~ $RE{time}{strftime}{-pat => '%b%d'};

Upvotes: 3

Related Questions