我有一个字符串:
__cfduid=d2eec71493b48565be764ad44a52a7b191399561601015; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.planetminecraft.com; HttpOnly
我想使用正则表达式并获取如下内容:
[0] = __cfduid=d2eec71493b48565be764ad44a52a7b191399561601015
[1] = expires=Mon, 23-Dec-2019 23:50:00 GMT
[2] = path=/
[3] = domain=.planetminecraft.com
[4] = HttpOnly
我尝试过此正则表达式:
[\A|;](.*?)[\Z|;]
我不明白为什么\A
。可以,但是[\A]
不可以,我如何创建(\A
或;
)?
在此正则表达式的最终形式中,我想从字符串中获取以下内容:
[0] = {
[0] = __cfduid
[1] = d2eec71493b48565be764ad44a52a7b191399561601015
}
[1] = {
[0] = expires
[1] = Mon, 23-Dec-2019 23:50:00 GMT
}
[2] = {
[0] = path
[1] = /
}
[3] = {
[0] = domain
[1] = .planetminecraft.com
}
[4] = {
[0] = HttpOnly
}
您可以尝试在此正则表达式上进行匹配:
\s*([^=;]+)(?:=([^=;]+))?
描述:
\s* # Match any spaces
([^=;]+) # Match any non = or ; characters
(?:
= # Match an = sign
([^=;]+) # Match any non = or ; characters.
)? # Make this group optional
在代码中:
string text = "__cfduid=d2eec71493b48565be764ad44a52a7b191399561601015; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.planetminecraft.com; HttpOnly";
var regex = new Regex(@"\s*([^=;]+)(?:=([^=;]+))?");
var matches = regex.Matches(text);
foreach (Match match in matches)
{
Console.WriteLine(match.Groups[1].Value + "\n" + match.Groups[2].Value + "\n");
}
\A
起作用,但[\A]
不是因为当您放入\A
字符类时,它像大多数正则表达式元字符一样失去了其含义。例如,+
并且*
也失去了意义。在中[\A]
,正则表达式实际上是在尝试匹配,\A
并且由于它在字符类中没有特定的含义,因此它表示文字A
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句