从字符串中取出日期
我的问题如下:
我有一个包含日期和其他数据的字符串数组。我的日期将采用以下几种格式之一:
- dd/mm/yyyy
- dd/mm/yy
- mm/yy
- d/m/yy
- yyyy
- yy
有没有办法在字符串中搜索适合字符串中该模式的数字?
另外,如果我可以检查 dd 是否在 1 到 31 之间(包含 1 和 31)等,那就太好了,但如果我事后必须这样做也不会那么糟糕。
My problem is as follows:
I have an array of strings that contain dates and other data. My date will have one of several formats:
- dd/mm/yyyy
- dd/mm/yy
- mm/yy
- d/m/yy
- yyyy
- yy
Is there a way to search a string for numbers that fit that pattern in the string?
In addition, it would be nice if I could check if the dd is between 1 and 31 inclusive etc, but it would not be so bad if I had to do that afterwards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其中每一个都对应一个正则表达式。
以下是每个的正则表达式:
\b(?:[012][1-9]|3[01])/(?:0[1-9]|1[012])/\d{4}\b
\b(?:[012][1-9]|3[01])/(?:0[1-9]|1[012])/\d{2}\b
\b(?:0[1-9]|1[012])/\d\d\b
\b[1-9]/[1-9]/\d\d\b
\b\d{4}\b
\b\d\d\b
当然,您可以通过不同的方式将它们组合在一起。您甚至可以制作一个超级正则表达式。
不过,最后一个相当有趣。我可以想象这样一种情况,您的文本中可能有一个普通的旧数字,例如
42
,它实际上可能与年份不对应。不过我想你可以对其进行后处理。快乐的调整。
ADDENDUM
回答评论中的一些问题:
是的,它在字符串的开头和结尾有效,因为
\b
是单词边界< /em>,其中包括从单词字符(字母、数字和下划线)到非单词字符的所有转换,反之亦然,包括字符串的开头和结尾。要查看测试,请参阅此处:http://jsfiddle.net/wRufK/。是的,我知道这是用 JavaScript 而不是 C# 编写的,但 jsfiddle 是显示实际代码的一种非常方便的方法。但还是有区别的——在 C# 中,我们使用
Regex.match
,而 JavaScript 正则表达式有额外的反斜杠来转义内部正斜杠。indexOf
可能过度杀伤力,具体取决于应用程序。如果要查找所有匹配项,请参阅 http://msdn.microsoft.com/ en-us/library/twcw2f1c.aspx 有关重复匹配的信息。您还可以修改用于捕获的正则表达式。由于您的日期可以采用上述任何形式,也可能采用其他形式,因此单个正则表达式可能更可取。一个非常灵活的日期查找器在这里: http://www.regular-expressions.info/dates.html 。您可能需要考虑它,而不是固定一个精确的集合。
Each of these corresponds to a regex.
Here are regexes for each:
\b(?:[012][1-9]|3[01])/(?:0[1-9]|1[012])/\d{4}\b
\b(?:[012][1-9]|3[01])/(?:0[1-9]|1[012])/\d{2}\b
\b(?:0[1-9]|1[012])/\d\d\b
\b[1-9]/[1-9]/\d\d\b
\b\d{4}\b
\b\d\d\b
Of course, you can combine these together in different ways. You can even make one super regex.
The last one is rather interesting, though. I can imagine a case where you might have a plain old number in your text, like
42
that might not actually correspond to a year. Still I guess you can postprocess that.Happy regexing.
ADDENDUM
To answer some questions in the comments:
Yes it works at the beginning and the end of the string, because
\b
is a word boundary, which includes all transitions from word characters (letters, digits, and underscores) to non-word characters and vice-versa, including the beginning and ending of the string.To see tests, see here: http://jsfiddle.net/wRufK/. Yes I know this is in JavaScript and not C#, but jsfiddle is a very convenient way to show code in action. There are differences though -- in C# we use
Regex.match
and the JavaScript regex has extra backslashes to escape the inner forward slashes.indexOf
might be overkill depending on the application. If you want to find all matches, see http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx for info on repeated matching. You can also modify the regexes for capturing.Since your dates can be in any of the forms above, and probably others, a single regex might be preferable. A very flexible date finder is here: http://www.regular-expressions.info/dates.html. You might want to consider it instead of fixing an exact set.