C# ISO 8601 时间间隔验证

发布于 2024-09-28 17:38:29 字数 370 浏览 0 评论 0原文

我搜索了一段时间,似乎找不到与此相关的任何内容。

我需要验证 ISO 8601 时间间隔。有很多关于 ISO 8601 日期时间的内容,但我特别需要间隔。我正在使用正则表达式,这就是我到目前为止所拥有的;

正则表达式 regEx = 新正则表达式(@"^P((\d+)Y)((\d+)M)((\d+)D)(T)((\d+)H)((\d+)M)(( \d+((.)?(\d)?(\d)?))S)$");

因此,基本上,这会检查输入字符串是否完全形成,例如 P0Y0M1DT1H2M2.01S 将被验证,但标准规定也应该接受较小的字符串(例如 PT4S)。

如果有人能帮助我,我将不胜感激。我一直在阅读条件正则表达式,但不太确定如何工作。

非常感谢。

I have searched for a while and can't seem to find anything that's related to this.

I need to validate an ISO 8601 time interval. There is loads of stuff about ISO 8601 date times but i specifically need interval. I am using a regex and this is what I have so far;

Regex regEx = new Regex(@"^P((\d+)Y)((\d+)M)((\d+)D)(T)((\d+)H)((\d+)M)((\d+((.)?(\d)?(\d)?))S)$");

So basically this checks if the input string is fully formed eg P0Y0M1DT1H2M2.01S would be validated but the standard states that smaller strings such as PT4S should be accepted too.

If anyone can help me with this I would be much appreciated. I've been reading up on conditional regex but not too sure how to work it.

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淡淡離愁欲言轉身 2024-10-05 17:38:29

我创建了一个正则表达式来获取有关 ISO 8601 中间隔的信息。查看一下:

^(P((?<Years>\d+)Y)?((?<Months>\d+)M)?((?<Days>\d+)D)?)(T((?<Hours>\d+)H)?((?<Minutes>\d+)M)?((?<Seconds>\d+((.)?(\d)?(\d)?))S)?)$

I have created a regular expression to get informations about interval in ISO 8601. Check it out:

^(P((?<Years>\d+)Y)?((?<Months>\d+)M)?((?<Days>\d+)D)?)(T((?<Hours>\d+)H)?((?<Minutes>\d+)M)?((?<Seconds>\d+((.)?(\d)?(\d)?))S)?)$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文