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

How can I extract date from a .txt file which name contains date? (Scala)

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

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

如何从文件名中提取日期 2018-10-01(10 月 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$