如何从文件名中提取 YYYYMMDD 格式的日期信息?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
my ($datepart)
创建列表上下文。列表上下文中的正则表达式匹配会返回捕获组。/(...)/
部分是一个捕获组。在内部,您将匹配年(4 位数字)、月份(2 位数字)和日(2 位数字),后跟字符串末尾的.txt
扩展名。我这样做是为了很容易更改为:
如果您决定单独需要组件。如果您想要的只是
YYYYMMDD
,也可以。
另请参阅 perldoc perlretut。
使用 split '.' 的问题很简单:split 的第一个参数是一个模式。在模式中
.
很特殊:它的意思是“匹配任何字符”。如果您使用split /[.]/
进行分割,则应将.
放入字符类中,删除特殊含义,并将其视为与自身匹配的字符。出于美观原因,我更喜欢split /\./
或split
qr{.}` 。正如 @TLP 所示,可以使用
split
来在本例中获得正确的部分,但最好使用m//
来确保您只匹配您想要匹配的内容。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:
if you decide you need the the components separately. If all you want is the
YYYYMMDD
,would also have worked.
See also perldoc perlretut.
The problem with using
split '.'
is simple: The first argument tosplit
is a pattern. In a pattern.
is special: It means "match any character". If you had split usingsplit /[.]/
, you would have put.
in a character class removing the special meaning, and treating it as a character that matches itself. I prefer that tosplit /\./
orsplit
qr{.}` 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 usem//
to ensure you are matching only what you want to match.下面是一个可以解决这个问题的正则表达式:
下面是它的实际操作:
括号捕获字符串并将其放入 $1 中。
注意:正则表达式可能会变得令人讨厌(在任何语言中),并且此正则表达式很容易出错,但如果您想做一些快速而肮脏的事情,那么可能没问题。
有很多关于 Perl 正则表达式的信息: perldocs
Here's a regular expression that does the trick:
And here's it in action:
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
使用 split 进行一些简单的字段操作
您根据下划线和句点拆分字符串,然后抓住第三个带下标的字段。
Some simple field manipulation with split
You split the string on underscore and period, and grab the third field with a subscript.
另一种方法是使用 substr() ,其中
9 表示从字符串的第 10 个字符开始(从 0 开始计数),8 是需要捕获的字符数。
Another way would be with substr()
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.