我有一个包含日期和文件大小的字符串。
上传了11-20 2017,尺寸11.93 GiB,已被P_O_O_P 取消(应与之匹配)
上传时间:11-20 15:20,尺寸11.93 GiB,已被P_O_O_P 取消(因为缺少日期年份,因此只能匹配尺寸)
2017年11月20日上传,尺码,以P_O_O_P 取消(仅限日期,因为尺寸不可用)
并为其创建了以下正则表达式
\b(\d{2}-\d{2}\s\d{4})\b.*?\b(\d+(?:\.\d+)?\s*[TGMK]iB)
我想在第0组中捕获日期11-20 2017
和第1组
大小11.93 GiB/MiB/TiB/KiB
。
有时日期不完整而没有一年。那么至少大小应该匹配。有时大小可能不可用,那么日期应该匹配。
因此,正则表达式应始终捕获字符串或日期或两者(取决于可用的内容)。
我尝试使用OR运算符|
,根据regex100它可行,但在我的应用程序中(QRegularExpression支持OR)组0和组1只是日期。
\b(\d{2}-\d{2}\s\d{4})\b.*?|\b(\d+(?:\.\d+)?\s*[TGMK]iB)
第0组“2017年9月27日”
第1组“2017年9月27日”
感谢您的帮助。
答案 0 :(得分:2)
您可以尝试(\d{2}-\d{2}\s\d{4})|(\d+(?:\.\d+)?\s(?:GiB|MiB|TiB|KiB))
这符合您的输入示例,如果您有任何其他条件我可以调整它。
(
\d{2} // two digis
- // a dash
\d{2} // two digits
\s // a whitespace character
\d{4} // four digits
)
| // Alternative
(
\d+ // one or more digits
(
?: // non capture group
\. // a . character
\d+ // one or more digits
)? // optional
\s // whitespace character
(
?: // non capture group
GiB|MiB|TiB|KiB // One of these
)
)