C# 获取字符串中的令牌块

C# 获取字符串中的令牌块,c#,C#,我的项目陷入困境,无法克服这个困难。我希望其他人能帮助我解决这个问题: 我有一个字符串,在这个字符串中有一些标记文本,我想手动将它们取出,并将它们放入字符串的数组列表中。最终结果可能有两个数组列表,一个是普通文本,另一个是标记文本。下面是一个字符串示例,其中包含一些由打开标记“[[”和关闭标记“]]”包围的标记 第一步,通过将淀粉源与热水混合来制备麦汁,称为[[Textarea]]。热水与压碎的麦芽或麦芽在糖化罐中混合。糖化过程大约需要[[复选框]],在此过程中淀粉转化为糖,然后甜麦汁从谷物中

我的项目陷入困境,无法克服这个困难。我希望其他人能帮助我解决这个问题:

我有一个字符串,在这个字符串中有一些标记文本,我想手动将它们取出,并将它们放入字符串的数组列表中。最终结果可能有两个数组列表,一个是普通文本,另一个是标记文本。下面是一个字符串示例,其中包含一些由打开标记“[[”和关闭标记“]]”包围的标记


第一步,通过将淀粉源与热水混合来制备麦汁,称为[[Textarea]]。热水与压碎的麦芽或麦芽在糖化罐中混合。糖化过程大约需要[[复选框]],在此过程中淀粉转化为糖,然后甜麦汁从谷物中排出。这些谷物现在通过一种称为[[无线电]]的过程进行洗涤。这种清洗使酿酒师能够从谷物中尽可能收集[[DropDownList]]可发酵液体


操作字符串后得到两个数组列表:

结果:

Normal Text ArrayList { "The first step, where the wort is prepared by mixing the starch source with hot water, is known as ", ". Hot water is mixed with crushed malt or malts in a mash tun. The mashing process takes around ", ", during which the starches are converted to sugars, and then the sweet wort is drained off the grains. The grains are now washed in a process known as ", ". This washing allows the brewer to gather ", " the fermentable liquid from the grains as possible." }

Token Text ArrayList { "[[Textarea]]", "[[CheckBox]]", "[[Radio]]", "[[DropDownList]]" }
两个数组列表,一个是普通文本数组列表,有5个元素是标记前后的文本,另一个是标记文本数组列表,有4个元素是字符串内部的标记文本

这项工作可以做哪种技术的削减和子字符串,但它是太长的文本难以,将很容易得到错误和一些时间无法得到我想要的。如果在这个问题上有一些帮助,请在C中发布,因为我正在使用C来完成这个任务。

这似乎可以完成任务(不过请注意,目前,我的
令牌
数组包含普通令牌,而不是用
[[
]
包装它们:

var inp = @"The first step, where the wort is prepared by mixing the starch source with hot water, is known as [[Textarea]]. Hot water is mixed with crushed malt or malts in a mash tun. The mashing process takes around [[CheckBox]], during which the starches are converted to sugars, and then the sweet wort is drained off the grains. The grains are now washed in a process known as [[Radio]]. This washing allows the brewer to gather [[DropDownList]] the fermentable liquid from the grains as possible.";

var step1 = inp.Split(new string[] { "[[" }, StringSplitOptions.None);
//step1 should now contain one string that's due to go into normal, followed by n strings which need to be further split
var step2 = step1.Skip(1).Select(a => a.Split(new string[] { "]]" }, StringSplitOptions.None));
//step2 should now contain pairs of strings - the first of which are the tokens, the second of which are normal strings.

var normal = step1.Take(1).Concat(step2.Select(a => a[1])).ToArray();
var tokens = step2.Select(a => a[0]).ToArray();
这还假设输入中没有不平衡的
[[[
]
序列

这个解决方案的观察结果是:如果您首先在原始文本中的每个
[[
对周围拆分字符串,那么第一个输出字符串已经生成。此外,在第一个字符串之后的每个字符串都包含一个标记,
]]
配对,一个普通文本。例如,
步骤1
中的第二个结果是:“Textarea]]。热水与压碎的麦芽或麦芽混合在一个捣碎桶中。捣碎过程大约需要


因此,如果您将这些其他结果拆分为
]
对,那么第一个结果是一个令牌,第二个结果是一个普通字符串。

是的。这太神奇了。这是我需要知道的问题。这是排序和计划,以获得错误泄漏。我已经测试了它,并根据我的需要获得最终结果。非常感谢您的帮助。对不起在通知之前发布我的答案已经有了答案。但是非常感谢你的帮助。