如何从文件名中提取 YYYYMMDD 格式的日期信息?

发布于 2024-12-11 06:06:47 字数 252 浏览 0 评论 0原文

我是 Perl 新手,有一个格式为 XXXX_XXX_YYYYMMDD.txt 的字符串。

如何提取另一个字符串中的 YYYYMMDD 部分?

以下是我尝试过的

my $filename = "XXXX_XXX_YYYYMMDD.txt";
my $datepart = split($filename ,'.');
print "$datepart";

I am new to Perl and have a string in the format XXXX_XXX_YYYYMMDD.txt.

How can I extract YYYYMMDD part in another string?

Below is what I tried

my $filename = "XXXX_XXX_YYYYMMDD.txt";
my $datepart = split($filename ,'.');
print "$datepart";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哭了丶谁疼 2024-12-18 06:06:48
 my ($datepart) = ( $filename =~ /([0-9]{4}[0-9]{2}[0-9]{2})[.]txt\z/ );

my ($datepart) 创建列表上下文。列表上下文中的正则表达式匹配会返回捕获组。 /(...)/ 部分是一个捕获组。在内部,您将匹配年(4 位数字)、月份(2 位数字)和日(2 位数字),后跟字符串末尾的 .txt 扩展名。

我这样做是为了很容易更改为:

 my ($year, $month, $day) = ( 
      $filename =~ /([0-9]{4}) ([0-9]{2}) ([0-9]{2})[.]txt\z/x
 );

如果您决定单独需要组件。如果您想要的只是 YYYYMMDD

 my ($datepart) = ( $filename =~ /([0-9]{8})[.]txt\z/ );

也可以。

另请参阅 perldoc perlretut

使用 split '.' 的问题很简单:split 的第一个参数是一个模式。在模式中 . 很特殊:它的意思是“匹配任何字符”。如果您使用 split /[.]/ 进行分割,则应将 . 放入字符类中,删除特殊含义,并将其视为与自身匹配的字符。出于美观原因,我更喜欢 split /\./splitqr{.}` 。

正如 @TLP 所示,可以使用 split 来在本例中获得正确的部分,但最好使用 m// 来确保您只匹配您想要匹配的内容。

 my ($datepart) = ( $filename =~ /([0-9]{4}[0-9]{2}[0-9]{2})[.]txt\z/ );

my ($datepart) creates list context. A regular expression match in list context returns capture groups. The part /(...)/ is a capture group. Inside, you are matching year (4 digits), month (2 digits), and day (2 digits) followed by the .txt extension at the end of the string.

I did it this way so that it is easy to change to:

 my ($year, $month, $day) = ( 
      $filename =~ /([0-9]{4}) ([0-9]{2}) ([0-9]{2})[.]txt\z/x
 );

if you decide you need the the components separately. If all you want is the YYYYMMDD,

 my ($datepart) = ( $filename =~ /([0-9]{8})[.]txt\z/ );

would also have worked.

See also perldoc perlretut.

The problem with using split '.' is simple: The first argument to split is a pattern. In a pattern . is special: It means "match any character". If you had split using split /[.]/, you would have put . in a character class removing the special meaning, and treating it as a character that matches itself. I prefer that to split /\./ or splitqr{.}` for aesthetic reasons.

As @TLP shows, it is possible to use split to get the correct part in this example, but it is better to use m// to ensure you are matching only what you want to match.

最笨的告白 2024-12-18 06:06:48

下面是一个可以解决这个问题的正则表达式:

/.{4}_.{3}_(.{8})/;

下面是它的实际操作:

"abcd_efg_12340322.txt" =~ /.{4}_.{3}_(.{8})/; 
print $1;

括号捕获字符串并将其放入 $1 中。

注意:正则表达式可能会变得令人讨厌(在任何语言中),并且此正则表达式很容易出错,但如果您想做一些快速而肮脏的事情,那么可能没问题。

有很多关于 Perl 正则表达式的信息: perldocs

Here's a regular expression that does the trick:

/.{4}_.{3}_(.{8})/;

And here's it in action:

"abcd_efg_12340322.txt" =~ /.{4}_.{3}_(.{8})/; 
print $1;

The parentheses capture the string and put it in $1.

Note: regexes can get nasty (in any language) and this regex could easily go wrong, but if you want to do something quick 'n dirty it might be all right.

There's lots of info out there on perl regexes: perldocs

尴尬癌患者 2024-12-18 06:06:48

使用 split 进行一些简单的字段操作

$date = (split /[_.]/, $filename)[2];

您根据下划线和句点拆分字符串,然后抓住第三个带下标的字段。

Some simple field manipulation with split

$date = (split /[_.]/, $filename)[2];

You split the string on underscore and period, and grab the third field with a subscript.

戏剧牡丹亭 2024-12-18 06:06:47

另一种方法是使用 substr() ,其中

my $txt = "abcd_efg_12340322.txt";
print substr($txt, 9, 8);

9 表示从字符串的第 10 个字符开始(从 0 开始计数),8 是需要捕获的字符数。

Another way would be with substr()

my $txt = "abcd_efg_12340322.txt";
print substr($txt, 9, 8);

The 9 means to start at the 10th character of the string (counting from 0), and the 8 is the number of chars you need to capture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文