我的字符串:
string str = "user:steo id:1 nickname|user:kevo id:2 nickname:kevo200|user:noko id:3 nickname";
现在,我想使用正则表达式获取值:
var reg = Regex.Matches(str, @"user:(.+?)\sid:(\d+)\s+nickname:(.+?)")
.Cast<Match>()
.Select(a => new
{
user = a.Groups[1].Value,
id = a.Groups[2].Value,
nickname = a.Groups[3].Value
})
.ToList();
foreach (var ca in reg)
{
Console.WriteLine($"{ca.user} id: {ca.id} nickname: {ca.nickname}");
}
我不知道如何使用正则表达式来使用nickname:(the nickname)
,我只想使用昵称,例如昵称为nickname:kevo200
和noch nickname
答案 0 :(得分:1)
我不确定这是否能回答您的问题,但我不确定100%,但是我通过正则表达式解析从给定的输入字符串中提取了一个列表,并在可用时返回昵称,否则返回用户名。
PS C:\WINDOWS\system32> scriptcs
> using System.Text.RegularExpressions;
> var regex = new Regex(@"\|?(?:user(?::?(?<user>\w+))\sid(?::?(?<id>\d*))\s?nickname(?::?(?<nick>\w+))?)");
> var matches = regex.Matches("user:steo id:1 nickname|user:kevo id:2 nickname:kevo200|user:noko id:3 nickname");
> matches.Cast<Match>().Select(m=>new {user=m.Groups["user"].Value,nick=m.Groups["nick"].Value}).Select(u=>string.IsNullOrWhiteSpace(u.nick)?u.user:u.nick);
[
"steo",
"kevo200",
"noko"
]
编辑:正则表达式设计器:https://regexr.com/3uf8t
edit:改进的版本,可以接受昵称中的转义序列
PS C:\WINDOWS\system32> scriptcs
> using System.Text.RegularExpressions;
> var regex = new Regex(@"\|?(?:user(?::(?<user>\w+))?\sid(?::(?<id>\d*))?\s?nickname(?::(?<nick>[\w\\]+))?)");
> var matches = regex.Matches("user:steo id:1 nickname|user:kevo id:2 nickname:kevo200|user:noko id:3 nickname|user:kevo id:2 nickname:kev\\so200");
> matches.Cast<Match>().Select(m=>new {user=m.Groups["user"].Value,nick=m.Groups["nick"].Value.Replace("\\s"," ")}).Select(u=>string.IsNullOrWhiteSpace(u.nick)?u.user:u.nick);
[
"steo",
"kevo200",
"noko",
"kev o200"
]
答案 1 :(得分:0)
尝试:user:(.+?)\sid:(\d+)\s+nickname:*(.*?)(\||$)
。
起初,我提出了此正则表达式:user:(.+?)\sid:(\d+)\s+nickname:*(.*?)\|*
–错误,由于延迟量词而无法捕获名称。
然后此正则表达式:user:(.+?)\sid:(\d+)\s+nickname(:(.+?)|)(\||$)
–这应匹配除以'|'的所有部分在您的字符串中,并给nickname =“”表示空的昵称。但是,如果未定义Groups[4]
(昵称后没有“:”),则需要检查值的存在。
答案 2 :(得分:0)
如果要由我决定并且您要处理的数据始终以管道分隔并且以恒定顺序进行,我可能会跳过正则表达式,并使用String.Split这样将字符串拆分成段。
string str = "user:steo id:1 nickname|user:kevo id:2 nickname:kevo200|user:noko id:3 nickname";
var entries = str.Split('|');
foreach(var entry in entries)
{
var subs = entry.Split(' ');
var userName = subs[0].Split(':')[1];
var id = subs[1].Split(':')[1];
var tempNick = subs[2].Split(':');
var nick = tempNick.Length == 2 ? tempNick[1] : string.Empty;
Console.WriteLine(userName + " id:" + id + " nickname " + nick);
}
答案 3 :(得分:0)
不使用正则表达式:
static void GetInfo()
{
string input = @"user:steo id:1 nickname|user:kevo id:2 nickname:kevo200|user:noko id:3 nickname";
var users =
from info in input.Split('|')
let x = info.Split(" ")
let nick_split = x[2].Split(':')
let has_nick = nick_split.GetUpperBound(0) > 0
let z = new
{
User = x[0].Split(':')[1],
Id = x[1].Split(':')[1],
Nickname = has_nick ? nick_split[1] : String.Empty
}
select z;
foreach (var user in users)
{
Console.WriteLine($"user: {user.User}, id: {user.Id}, nickname: {user.Nickname}");
}
}