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:
我想将字符串中的时间(10.00)与日期和时间(“21.01.08 10.00”)进行匹配。我正在使用以下正则表达式:
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.
但这从21.01.08和10.00匹配21.01。
I'm using PCRE as my regualar expression engine.
我正在使用PCRE作为我的regualar表达引擎。
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"
“从2010年10月21日开始,派对将在图书馆举行”“21.08.08 - 在10:00将有一个派对”“在2008年8月21日,你将被安排...... ......将...从10.00开始“
Is this possible?
这可能吗?
3 个解决方案
#1
4
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:
您的原始正则表达式不起作用,因为\ b(单词边界)匹配“。”在“21.01.08。”您需要更健壮地编码边界:
(?:[^\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.
这会捕获您使用的任何一种符号中的时间,同时排除日期。请注意,它不会验证时间。例如,它将匹配“88:99”验证时间是可能的但是显着地使模式复杂化并且在大多数情况下可能是过度杀伤。
It would be nice to use a look-behind instead of the non-capturing grouping but PCRE don't support variable-width look-behind.
使用后视而不是非捕获分组会很好,但PCRE不支持可变宽度的后视。
#2
1
^\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...
“新的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;
}
in .NET the following should work...
在.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();
You could also use a Named Capture if you want to use something less ambiguous then the index into the capture group.
如果您想使用比索引更不明确的东西,您还可以使用命名捕获。
#3
0
try using
new RegExp("\\b[0-9]{1,2}\\.[0-9]{1,2}$" "g");
$ indicates end of string
$表示字符串结束
#1
4
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:
您的原始正则表达式不起作用,因为\ b(单词边界)匹配“。”在“21.01.08。”您需要更健壮地编码边界:
(?:[^\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.
这会捕获您使用的任何一种符号中的时间,同时排除日期。请注意,它不会验证时间。例如,它将匹配“88:99”验证时间是可能的但是显着地使模式复杂化并且在大多数情况下可能是过度杀伤。
It would be nice to use a look-behind instead of the non-capturing grouping but PCRE don't support variable-width look-behind.
使用后视而不是非捕获分组会很好,但PCRE不支持可变宽度的后视。
#2
1
^\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...
“新的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;
}
in .NET the following should work...
在.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();
You could also use a Named Capture if you want to use something less ambiguous then the index into the capture group.
如果您想使用比索引更不明确的东西,您还可以使用命名捕获。
#3
0
try using
new RegExp("\\b[0-9]{1,2}\\.[0-9]{1,2}$" "g");
$ indicates end of string
$表示字符串结束