事实是,我很难编写正则表达式字符串来解析格式为
[[[tab name=dog content=cat|tab name=dog2 content=cat2]]]这个正则表达式将被解析,这样我就可以动态地构建选项卡,如下所示。最初,我尝试了像\[\[\[tab name=(?'name'.*?) content=(?'content'.*?)\]\]\]这样的正则表达式模式
但我意识到,如果不执行regex.replace,我就无法获得整个选项卡并在查询的基础上构建。是否可以将通向管道符号的整个制表符作为一个组,然后从子键/值对中向下解析该组?
这是我正在使用的\[\[\[(?'tab'tab name=(?'name'.*?) content=(?'content'.*?))\]\]\]的当前正则表达式字符串
下面是我执行正则表达式的代码。任何指导都将不胜感激。
public override string BeforeParse(string markupText)
{
if (CompiledRegex.IsMatch(markupText))
{
// Replaces the [[[code lang=sql|xxx]]]
// with the HTML tags (surrounded with {{{roadkillinternal}}.
// As the code is HTML encoded, it doesn't get butchered by the HTML cleaner.
MatchCollection matches = CompiledRegex.Matches(markupText);
foreach (Match match in matches)
{
string tabname = match.Groups["name"].Value;
string tabcontent = HttpUtility.HtmlEncode(match.Groups["content"].Value);
markupText = markupText.Replace(match.Groups["content"].Value, tabcontent);
markupText = Regex.Replace(markupText, RegexString, ReplacementPattern, CompiledRegex.Options);
}
}
return markupText;
}发布于 2015-09-11 00:01:52
这是你想要的吗?
string input = "[[[tab name=dog content=cat|tab name=dog2 content=cat2]]]";
Regex r = new Regex(@"tab name=([a-z0-9]+) content=([a-z0-9]+)(\||])");
foreach (Match m in r.Matches(input))
{
Console.WriteLine("{0} : {1}", m.Groups[1].Value, m.Groups[2].Value);
}http://regexr.com/3boot
发布于 2015-09-11 00:04:21
也许在这种情况下string.split会更好?例如,类似这样的内容:
strgin str = "[[[tab name=dog content=cat|tab name=dog2 content=cat2]]]";
foreach(var entry in str.Split('|')){
var eqBlocks = entry.Split('=');
var tabName = eqBlocks[1].TrimEnd(" content");
var content = eqBlocks[2];
}丑陋的代码,但应该可以工作。
发布于 2015-09-11 01:16:57
https://stackoverflow.com/questions/32506541
复制相似问题