正则表达式从列表中过滤日期
我有一个如下所示的列表:
['','2022-03-31', 'euss', 'projects','2021-03-31']
我想编写一个正则表达式,以便从列表中删除所有其他项目,只保留那些具有日期格式的项目。例如,2022 年 3 月 31 日和 2021 年 3 月 31 日。
我尝试了这个,但当我打印列表时,它似乎没有什么区别:
my_list = [re.sub(r"(\d+)/(\d+)/(\d+)",r"\3-\1-\2",i) for i in list(my_list) ]
I have a list that looks like this:
['','2022-03-31', 'euss', 'projects','2021-03-31']
I want to write a regular expression such that I delete all other items from the list and only keep those that have a date format. For example, 2022-03-31 and 2021-03-31.
I tried this but it doesn't seem to make a difference when I print my list out:
my_list = [re.sub(r"(\d+)/(\d+)/(\d+)",r"\3-\1-\2",i) for i in list(my_list) ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以首先检查正则表达式是否与字符串匹配:
请参阅 Python 演示。
输出:
您可以将其写为:
You can first check if there regex matches the string:
See the Python demo.
Output:
You may write it as: