将时间字符串与正则表达式匹配

发布于 2024-07-07 23:14:59 字数 434 浏览 2 评论 0原文

我想将字符串中的时间(10.00)与日期和时间(“21.01.08 10.00”)进行匹配。 我使用以下正则表达式:

new RegExp("\\b[0-9]{1,2}\\.[0-9]{1,2}\\b" "g");

但这与 21.01.08 和 10.00 中的 21.01 匹配。

我使用 PCRE 作为我的正则表达式引擎。

更新:

对不起,我应该更清楚。 数据和时间是较大字符串的一部分。 我想从该字符串中提取时间。

例如:

“2008 年 1 月 21 日 10 点,图书馆将举办一场聚会” “2008 年 8 月 21 日 - 10:00 将举办一场派对” “2008 年 8 月 21 日,您计划...。...将于 10 点开始”

这可能吗?

I would like to match the time (10.00) from a string with the date and time ("21.01.08 10.00"). I'm using the following regular expression:

new RegExp("\\b[0-9]{1,2}\\.[0-9]{1,2}\\b" "g");

But this matches 21.01 from 21.01.08 and 10.00.

I'm using PCRE as my regualar expression engine.

Update:

I'm sorry, i should have more been more clear. The data and time are part of a larger string. I want to extract the time from that string.

For example:

"On 21.01.08 from 10.00 a party will take place in the library"
"21.08.08 - At 10:00 there will be a party"
"On 21.08.08 you are scheduled for a ... . The ... will begin at 10.00"

Is this possible?

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

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

发布评论

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

评论(3

甜警司 2024-07-14 23:14:59

您原来的正则表达式不起作用,因为 \b (单词边界)在“.”处匹配。 在“2008 年 1 月 21 日”。 您需要更可靠地对边界进行编码:

(?:[^\d:.]|^)(\d\d?[.:]\d\d)(?![.:\d])

这会以您使用的任何一种表示法捕获时间,同时排除日期。 请注意,它不会验证时间。 例如,它将匹配“88:99” 验证时间是可能的,但会使模式显着复杂化,并且对于大多数情况来说可能有点过分了。

使用后视而不是非捕获分组会很好,但 PCRE 不支持可变宽度后视。

Your original regex didn't work because \b (word boundary) matches at the "." in "21.01.08." You need to code the boundaries more robustly:

(?:[^\d:.]|^)(\d\d?[.:]\d\d)(?![.:\d])

This captures the time, in either of the notations you used, while excluding dates. Note that it does not validate the time. For example, it would match "88:99" Validating the time is possible but complicates the pattern significantly and is likely to be overkill for most situations.

It would be nice to use a look-behind instead of the non-capturing grouping but PCRE don't support variable-width look-behind.

故事和酒 2024-07-14 23:14:59
^\d{2}\.\d{2}\.\d{2}\s(\d{2}\.\d{2})$

应该将时间部分放入捕获组中来解决问题。

我不确定“new RegExp”(也许是Java?)。 在 Perl 中,您可以获得类似的值...

if ("21.01.08 10.00" =~ m/^\d{2}\.\d{2}\.\d{2}\s(\d{2}\.\d{2})$/g) {
  $time_part = $1;
}

在 .NET 中,以下内容应该有效...

  Regex r = new Regex(@"^\d{2}\.\d{2}\.\d{2}\s(\d{2}\.\d{2})$");
  string dateTimeString = "21.01.08 10.00";
  if (r.IsMatch(dateTimeString)) {
    string timePart = r.Match(dateTimeString).Groups[1].Value;
    Console.Write(timePart);
  }
  Console.ReadKey();

您还可以使用 命名捕获 如果您想使用更明确的内容,则使用捕获组的索引。

^\d{2}\.\d{2}\.\d{2}\s(\d{2}\.\d{2})$

should do the trick with the time part being put in a capture group.

the "new RegExp" I'm not sure about (Java perhaps?). In Perl you could get the value like...

if ("21.01.08 10.00" =~ m/^\d{2}\.\d{2}\.\d{2}\s(\d{2}\.\d{2})$/g) {
  $time_part = $1;
}

in .NET the following should work...

  Regex r = new Regex(@"^\d{2}\.\d{2}\.\d{2}\s(\d{2}\.\d{2})$");
  string dateTimeString = "21.01.08 10.00";
  if (r.IsMatch(dateTimeString)) {
    string timePart = r.Match(dateTimeString).Groups[1].Value;
    Console.Write(timePart);
  }
  Console.ReadKey();

You could also use a Named Capture if you want to use something less ambiguous then the index into the capture group.

┊风居住的梦幻卍 2024-07-14 23:14:59

尝试使用

new RegExp("\\b[0-9]{1,2}\\.[0-9]{1,2}$" "g");

$ 表示字符串结尾

try using

new RegExp("\\b[0-9]{1,2}\\.[0-9]{1,2}$" "g");

$ indicates end of string

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