与正则表达式匹配第一个日期 [英] Match first date in line with regex

查看:293
本文介绍了与正则表达式匹配第一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Notepad ++写一个正则表达式搜索字符串,以匹配每行的第一个日期。

I'm trying to write a regex search string for Notepad++ that will match the first date on every line.

我的文字看起来像这样:

My text looks something like this:


Nåskal民谣, iTromsø,09.09.2017 19:09 MartinLægland,Publisertpånett。
Nåskal民谣,iTromsø,09.09.2017,MartinLægland2017年12月31日Publisertpånett。

Nåskal民谣杂志,iTromsø,2017年9月9日19:09 MartinLægland,Publisertpånett。

Nå skal folk få fred, iTromsø, 09.09.2017 19:09 Martin Lægland, Publisert på nett.
Nå skal folk få fred, iTromsø, 09.09.2017, Martin Lægland 31.12.2017 Publisert på nett.
Nå skal folk få fred, iTromsø, 09.09.2017 19:09 Martin Lægland, Publisert på nett.

我只想要每行的第一个日期,因此排除第二行的 2017年12月31日

I only want the first date on each line, so excluding 31.12.2017 on the second line.

我已经尝试过 \K(([0-9] {2})。([0-9] {2}) 。([0-9] {4})),但这只给了我第二个约会,而不是第一个。

I've tried \K(([0-9]{2}).([0-9]{2}).([0-9]{4})), but that only gives me the second date, not the first.

推荐答案

代码



在此处查看正则表达式

^.*?\K\d+\.\d+\.\d+






结果



输入




Results

Input

Nå skal folk få fred, iTromsø, 09.09.2017 19:09 Martin Lægland, Publisert på nett.
Nå skal folk få fred, iTromsø, 09.09.2017, Martin Lægland 31.12.2017 Publisert på nett.
Nå skal folk få fred, iTromsø, 09.09.2017 19:09 Martin Lægland, Publisert på nett.



输出



Output

09.09.2017
09.09.2017
09.09.2017






说明




  • ^ 声明位置在行的开头

  • 。*?任意次数匹配任意字符,但次数尽可能少

  • \K 重置报告的匹配项的起点。

  • \d + \.\d + \.\d + >匹配任意一位或多位数字,后跟一个点从字面上看,然后是相同的事物(任意一位或多位,后跟一个点字面意思),然后是任意一个或多次数字


  • Explanation

    • ^ Assert position at the start of the line
    • .*? Match any character any number of times, but as few as possible
    • \K Reset the starting point of the reported match. Any previously consumed characters are no longer included in the final match
    • \d+\.\d+\.\d+ Match any digit one or more times, followed by a dot . literally, followed by the same thing (any digit one or more times, followed by a dot . literally), followed by any digit one or more times
    • 这篇关于与正则表达式匹配第一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆