简体   繁体   English

RegEx替换查询以选择Wiki语法

[英]RegEx replace query to pick out wiki syntax

I've got a string of HTML that I need to grab the "[Title| http://www.test.com] " pattern out of eg 我有一个HTML字符串,我需要从例如HTML中获取“ [Title | http://www.test.com] ”模式

"dafasdfasdf, adfasd. [Test| http://www.test.com/] adf ddasfasdf [SDAF| http://www.madee.com/] assg ad" “ dafasdfasdf,adfasd。[测试| http://www.test.com/] adf ddasfasdf [SDAF | http://www.madee.com/] assg广告”

I need to replace "[Title| http://www.test.com] " this with "http://www.test.com/'>Title". 我需要将“ [Title | http://www.test.com] ”替换为“ http://www.test.com/'>Title”。

What is the best away to approach this? 解决这个问题的最佳途径是什么?

I was getting close with: 我越来越接近:

string test = "dafasdfasdf adfasd [Test|http://www.test.com/] adf ddasfasdf [SDAF|http://www.madee.com/] assg ad ";
        string p18 = @"(\[.*?|.*?\])";
        MatchCollection mc18 = Regex.Matches(test, p18, RegexOptions.Singleline | RegexOptions.IgnoreCase);
        foreach (Match m in mc18)
        {
            string value = m.Groups[1].Value;
            string fulltag = value.Substring(value.IndexOf("["), value.Length - value.IndexOf("["));
            Console.WriteLine("text=" + fulltag);
        }

There must be a cleaner way of getting the two values out eg the "Title" bit and the url itself. 必须有一种更干净的方法来获取两个值,例如“ Title”位和url本身。

Any suggestions? 有什么建议么?

Replace the pattern: 替换模式:

\[([^|]+)\|[^]]*]

with: 有:

$1

A short explanation: 简短说明:

\[         # match the character '['
(          # start capture group 1
  [^|]+    #   match any character except '|' and repeat it one or more times
)          # end capture group 1
\|         # match the character '|'
[^]]*      # match any character except ']' and repeat it zero or more times
]          # match the character ']'

AC# demo would look like: AC#演示看起来像:

string test = "dafasdfasdf adfasd [Test|http://www.test.com/] adf ddasfasdf [SDAF|http://www.madee.com/] assg ad ";
string adjusted = Regex.Replace(test, @"\[([^|]+)\|[^]]*]", "$1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM