[英]Regex to match a 28 character ID string with special characters
我有一個小應用程序,可以讀取另一個應用程序制作的一些日志文件。 在這些文件中,正在處理類似於以下內容的行:
ban added reason='Posting links to malware websites' cluid='oNNtrNGo6kdxNRshT8MiHlq4wR8=' bantime=0 by client 'Someone'(id:4)
目前,我有一點Regex \\w{27}=
可以獲取該字符串中的線索值。 線索的長度始終為27個字符,結尾處帶有'='。 但是,其中一些ID本身具有特殊字符,例如: IVz0tUZThCdbBnCWjf+axoMqVTM=
(注意'+'字符)這意味着我的正則表達式與此ID不匹配。
我需要在正則表達式中添加什么才能使其與兩個ID匹配?
您只對線索的值感興趣(介於單引號之間)。 您可以嘗試以下模式:
"cluid='([^']{27}=)'"
它捕獲了不是單引號的27個字符(假設單引號不能是值的一部分),然后捕獲到捕獲組1中的等號。
例:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string line1 = "ban added reason='Posting links to malware websites' cluid='oNNtrNGo6kdxNRshT8MiHlq4wR8=' bantime=0 by client 'Someone'(id:4)";
Match m = Regex.Match(line1, "cluid='([^']{27}=)'");
if (m.Success)
{
Console.WriteLine(m.Groups[1]);
}
string line2 = "ban added reason='Posting links to malware websites' cluid='IVz0tUZThCdbBnCWjf+axoMqVTM=' bantime=0 by client 'Someone'(id:4)";
m = Regex.Match(line2, "cluid='([^']{27}=)'");
if (m.Success)
{
Console.WriteLine(m.Groups[1]);
}
}
}
結果:
oNNtrNGo6kdxNRshT8MiHlq4wR8=
IVz0tUZThCdbBnCWjf+axoMqVTM=
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.