我在本地目录中有多个文件,名称如下:
[^+]
yyyyMMdd表示日期。 还有一些名为:
的文件asd-3A-yyyyMMdd
还有一堆不同名字的文件,对我来说是不必要的。 如何仅从以 asd 开头的文件中提取日期? 我尝试的任何东西似乎都没有用。
答案 0 :(得分:4)
这个正则表达式
asd-[0-9][a-z]-([0-9]{4})([0-9]{2})([0-9]{2})
将执行以下操作
asd-
-
注意:此正则表达式不会验证日期是否合法。
给出以下示例文本
bsd-3A-20170523
asd-3A-20170523
NotTheDroidsYourLookingFor-20171131
asd-1D-20170523
返回以下匹配
Match 1
Full match 16-31 `asd-3A-20170523`
Group 1. 23-27 `2017`
Group 2. 27-29 `05`
Group 3. 29-31 `23`
Match 2
Full match 68-83 `asd-1D-20170523`
Group 1. 75-79 `2017`
Group 2. 79-81 `05`
Group 3. 81-83 `23`
NODE EXPLANATION
--------------------------------------------------------------------------------
asd- 'asd-'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[0-9]{4} any character of: '0' to '9' (4 times)
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
--------------------------------------------------------------------------------
) end of \3