我想明白,如果我的消息,包括像如何檢查是否一個文本包含使用C#正則表達式
<number>:<number>
一組固定的字符的是什麼,我想是一個集焦炭:
if (!loggingEvent.RenderedMessage.Contains("[0-9]:[0-9]"))
{
...
}
但它不工作,因爲我想。我該如何解決它?它在C#中。
編輯
整個字符串是這樣的:
The server IP is -> 127.1.2.35:9001!
我想明白,如果我的消息,包括像如何檢查是否一個文本包含使用C#正則表達式
<number>:<number>
一組固定的字符的是什麼,我想是一個集焦炭:
if (!loggingEvent.RenderedMessage.Contains("[0-9]:[0-9]"))
{
...
}
但它不工作,因爲我想。我該如何解決它?它在C#中。
編輯
整個字符串是這樣的:
The server IP is -> 127.1.2.35:9001!
一個正則表達式的方法看起來像
if (!Regex.IsMatch(loggingEvent.RenderedMessage, "[0-9]+:[0-9]+"))
注意String.Contains
不支持正則表達式。此外,[0-9]
匹配1位數,而您可能想要允許1個或更多(這是+
確保)。
見the online C# demo還提取子字符串:
var s = "The server IP is -> 127.1.2.35:9001!";
var result = Regex.Match(s, @"[0-9]+:[0-9]+");
if (result.Success)
Console.WriteLine(result.Value);
else
Console.WriteLine("No match!");
Regex regex = new Regex(@"[0-9]:[0-9]");
Match match = regex.Match("<number>:<number>");
if (match.Success)
{
Console.WriteLine(match.Value);
}
Ty for your answer。我編輯了這個問題,請你檢查一下 –
應該像'如果(Regex.IsMatch(loggingEvent.RenderedMessage,「^ [0-9] +:[0-9 ] + $「))'。 'String.Contains'不支持正則表達式。另外,'[0-9]'匹配1個數字,而您可能想要允許1個或更多(這是'+'確保的)。 '^'和'$'將模式定位到字符串start/end。當然,你可以用':'分割字符串,並檢查兩個分割值是否都是數字。 –