我有一种字符串,
< 155> 33739:033910:* 12月12日01:09:12.669 UTC:%test1-3-test2 :您好,你的时代
下面,
在regex表达式匹配下,结果为 false ,需要在regex表达式中进行更改。谢谢!
private static bool IsValid(string value)
{
const string expression = @"(\w+):(\w+):(\w+):%(\w+)-(\d)(\w+):(\w+)";
return Regex.IsMatch(value, expression, RegexOptions.IgnoreCase);
}
var X = IsValid("<155>33739: 033910: *Dec 12 01:09:12.669 UTC: %XYz-3-UPDxyt: Hello, How era you");
答案 0 :(得分:2)
您的正则表达式缺少冒号后的空格,最初的5
和<
以及特殊的字符,例如'*'(如果我没记错的话),{{ 1}}。试试这个:
输入:
>
正则表达式:
\w
匹配:
<155>33739: 033910: *Dec 12 01:09:12.669 UTC: %XYz-3-UPDxyt: Hello, How era you
C#代码v1,索引组:
<(\d+)>(\d+):\s+(\d+):\s+([^%]+)%([A-Za-z]+)-(\d+)-([A-Za-z]+):\s+([\w,\s]+)
C#代码v2,命名组,将#0 '<155>33739: 033910: *Dec 12 01:09:12.669 UTC: %XYz-3-UPDxyt: Hello, How era you'
#1 '155'
#2 '33739'
#3 '033910'
#4 '*Dec 12 01:09:12.669 UTC: '
#5 'XYz'
#6 '3'
#7 'UPDxyt'
#8 'Hello, How era you'
放入组:
var input = @"<155>33739: 033910: *Dec 12 01:09:12.669 UTC: %XYz-3-UPDxyt: Hello, How era you";
var regex = new Regex(@"<(\d+)>(\d+):\s+(\d+):\s+([^%]+)%([A-Za-z]+)-(\d+)-([A-Za-z]+):\s+([\w,\s]+)");
var match = regex.Match(input);
if (match.Success)
{
var date = match.Groups[4];
}
似乎将命名组移动到匹配组的末尾。所以要么全部命名,要么不命名......