开发者

RegEx replace query to pick out wiki syntax

开发者 https://www.devze.com 2022-12-30 04:15 出处:网络
I\'ve got a string of HTML that I need to grab the \"[Title|http://www.test.com]\" pattern out of e.g.

I've got a string of HTML that I need to grab the "[Title|http://www.test.com]" pattern out of e.g.

"dafasdfasdf, adfasd. [Test|http://www.test.com/] adf ddasfasdf [SDAF|http://www.madee.com/] assg ad"

I need to replace "[Title|http://www.test.com]" this with "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 e.g. the "Title" bit and the url itself.

Any开发者_Python百科 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 ']'

A C# demo would look like:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号