如何从名称包含日期的.txt文件中提取日期? (斯卡拉)

问题描述 投票:1回答:1

我有一个.txt文件作为我的光束编程项目的输入,使用scala spotify scio。

input= args.getOrElse("input", "/home/user/Downloads/trade-20181001.txt")

如何从文件名中提取日期2018 - 10 - 01(10月1日)?谢谢!

regex scala apache-beam spotify-scio
1个回答
1
投票

在上面的例子中,我只想使用下面的正则表达式。它搜索以8个数字结尾的任何内容,后跟.txt。

(?<dateTime>\d{8})\.txt$

(?<dateTime> is the start of a named capture group.
\d{8} means exactly 8 digits.
) is the end of the named capture group.
\. means match the character . literally.
txt means match txt literally.
$ means that the string ends there and nothing comes after it.

如果您不能在程序中使用命名捕获组,则可以在不使用它的情况下使用下面的正则表达式,并将其替换为.txt。

\d{8}\.txt$
© www.soinside.com 2019 - 2024. All rights reserved.