使用正则表达式括号之间提取字符串,即{{内容}}括号、字符串、内容、正则表达式

由网友(年少德、无知)分享简介:我因为有占位符的格式字符串{{some_text}}。我想用C#来提取这一到一个集合,并相信正则表达式是做到这一点的最好办法。正则表达式是有点超过我的头,但似乎足以在这种情况下工作。这是我的例子:I am given a string that has place holders in the format of {...

我因为有占位符的格式字符串{{some_text}}。我想用C#来提取这一到一个集合,并相信正则表达式是做到这一点的最好办法。正则表达式是有点超过我的头,但似乎足以在这种情况下工作。这是我的例子:

I am given a string that has place holders in the format of {{some_text}}. I would like to extract this into a collection using C# and believe RegEx is the best way to do this. RegEx is a little over my head but it seems powerful enough to work in this case. Here is my example:

<a title="{{element='title'}}" href="{{url}}">
<img border="0" alt="{{element='title'}}" src="{{element='photo' property='src' maxwidth='135'}}" width="135" height="135" /></a>
<span>{{element='h1'}}</span>
<span><strong>{{element='price'}}<br /></strong></span>

我想最终的东西是这样的:

I would like to end up with something like this:

收集[0] =元素='称号';

collection[0] = "element='title'";

集合[1] =URL;

collection[1] = "url";

集合[2] =元素='照片'属性='src'中了maxWidth ='135';

collection[2] = "element='photo' property='src' maxwidth='135'";

集合[3] =元素=H1;

collection[3] = "element='h1'";

集合[4] =元素='价';

collection[4] = "element='price'";

请注意,有没有重复下去,但我不想复杂的事情,如果它是很难做到的。

Notice that there are no duplicates either, but I do not want to complicate things if it is difficult to do.

我看到这个帖子,做类似的事情,但括号内: http://stackoverflow.com/questions/1811183/how-to-extract-the-contents-of-square-brackets-in-a-string-of-text-in-c-using-re

I saw this post that does something similar but within brackets: http://stackoverflow.com/questions/1811183/how-to-extract-the-contents-of-square-brackets-in-a-string-of-text-in-c-using-re

我在这里的问题是,我有双重支撑,而不只是一个字符。我怎样才能做到这一点?

My problem here is that I have double braces instead of just one character. How can I do this?

推荐答案

以完全从你链接的问题:

Taking exactly from the question you linked:

ICollection<string> matches =
    Regex.Matches(s.Replace(Environment.NewLine, ""), @"{{([^}]*)}}")
        .Cast<Match>()
        .Select(x => x.Groups[1].Value)
        .ToList();

foreach (string match in matches)
    Console.WriteLine(match);

我已经改变了[和]为{{和}}(逃脱)。这应该使你所需要的集合。请务必阅读第一个回答其他问题的正则表达式击穿。重要的是要了解它,如果你使用它。

I've changed the [ and ] to {{ and }} (escaped). This should make the collection you need. Be sure to read the first answer to the other question for the regex breakdown. It's important to understand it if you use it.

阅读全文

相关推荐

最新文章