假设我有一个像这样的字符串:
“我的名字是 <
如何提取 <
我的名字是
<
我在
工作
<
团队
作为一个
<
.
备注:
我想在C#中使用Regex,但没有成功。
您可以使用正则表达式来平衡此SO帖子中的字符。调整后如下所示:
\<\<(?>\<\<(?<c>)|[^<>]+|\>\>(?<-c>))*(?(c)(?!))\>\>
它将数学平衡
<< .... >>
代币。
然后您可以使用
Regex.Matches
和Regex.Split
来获取您想要的所有物品:
var regex = new Regex(@"\<\<(?>\<\<(?<c>)|[^<>]+|\>\>(?<-c>))*(?(c)(?!))\>\>");
var searchString = "My name is <<yourName>> and I am working in the <<Team Name>> team\n as a <<yourTitle>>.";
var matches = regex.Matches(searchString);
var parts = regex.Split(searchString);