从字符串中取出日期

发布于 2024-11-29 14:36:16 字数 280 浏览 0 评论 0原文

我的问题如下:

我有一个包含日期和其他数据的字符串数组。我的日期将采用以下几种格式之一:

  1. dd/mm/yyyy
  2. dd/mm/yy
  3. mm/yy
  4. d/m/yy
  5. yyyy
  6. 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:

  1. dd/mm/yyyy
  2. dd/mm/yy
  3. mm/yy
  4. d/m/yy
  5. yyyy
  6. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一直都在从未离去 2024-12-06 14:36:16

其中每一个都对应一个正则表达式。

以下是每个的正则表达式:

  • dd/mm/yyyy ==> \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
  • d/m/yy ==> \b[1-9]/[1-9]/\d\d\b
  • yyyy ==> \b\d{4}\b
  • yy ==> \b\d\d\b

当然,您可以通过不同的方式将它们组合在一起。您甚至可以制作一个超级正则表达式。

不过,最后一个相当有趣。我可以想象这样一种情况,您的文本中可能有一个普通的旧数字,例如 42,它实际上可能与年份不对应。不过我想你可以对其进行后处理。

快乐的调整。

ADDENDUM

回答评论中的一些问题:

  1. 是的,它在字符串的开头和结尾有效,因为\b单词边界< /em>,其中包括从单词字符(字母、数字和下划线)到非单词字符的所有转换,反之亦然,包括字符串的开头和结尾。

  2. 要查看测试,请参阅此处:http://jsfiddle.net/wRufK/。是的,我知道这是用 JavaScript 而不是 C# 编写的,但 jsfiddle 是显示实际代码的一种非常方便的方法。但还是有区别的——在 C# 中,我们使用 Regex.match,而 JavaScript 正则表达式有额外的反斜杠来转义内部正斜杠。

  3. indexOf 可能过度杀伤力,具体取决于应用程序。如果要查找所有匹配项,请参阅 http://msdn.microsoft.com/ en-us/library/twcw2f1c.aspx 有关重复匹配的信息。您还可以修改用于捕获的正则表达式。

  4. 由于您的日期可以采用上述任何形式,也可能采用其他形式,因此单个正则表达式可能更可取。一个非常灵活的日期查找器在这里: http://www.regular-expressions.info/dates.html 。您可能需要考虑它,而不是固定一个精确的集合。

Each of these corresponds to a regex.

Here are regexes for each:

  • dd/mm/yyyy ==> \b(?:[012][1-9]|3[01])/(?:0[1-9]|1[012])/\d{4}\b
  • dd/mm/yy ==> \b(?:[012][1-9]|3[01])/(?:0[1-9]|1[012])/\d{2}\b
  • mm/yy ==> \b(?:0[1-9]|1[012])/\d\d\b
  • d/m/yy ==> \b[1-9]/[1-9]/\d\d\b
  • yyyy ==> \b\d{4}\b
  • yy ==> \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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文