RegEx查找项目之前没有

时间:2014-07-07 15:18:46

标签: c# regex

我试图通过Regex(不是xml)从这个元素中获取值。我需要元素中的单词Description。

<ValueExpressionLeft xmlns="http://Microsoft.EnterpriseManagement.Core.Criteria/">
 <Property>$Context/Property[Type='WorkItem!System.WorkItem']/Description$</Property>
</ValueExpressionLeft>

这只返回type =

后出现的内容
 string typeString = Regex.Match(valueExpressionLeft.Value.ToString(), "(?<=Type=\')[^\']+").Value;

我需要在]/

之后获得单词描述

我在下面尝试了kessly的subbestion无济于事:

value left only containts this 
$Context/Property[Type='WorkItem!System.WorkItem']/Description$

string left =valueExpressionLeft.Value.ToString();

string fieldname =Regex.Match(left,@"\[Type=\'.*\']/([^<]+)[</]").Value;

1 个答案:

答案 0 :(得分:0)

我假设您正在寻找单词Description$

如果是这样,以下正则表达式应该可以获得数据:

\[Type=\'.*\'\]/([^<]+)[</]

组$ 1:Description$

编辑:要回答您的评论,是的,它确实可以在.net中使用。这是完整的示例,但实际上您应该阅读Regex文档:

string parseText = "<ValueExpressionLeft xmlns='http://Microsoft.EnterpriseManagement.Core.Criteria/'><Property>$Context/Property[Type='WorkItem!System.WorkItem']/Description$</Property></ValueExpressionLeft>";
Match match = Regex.Match(parseText, @"\[Type=\'.*\'\]/([^<]+)[</]");
string field = match.Groups[1].Value;

如果您想在最后删除$,可以使用以下正则表达式:

\[Type=\'.*\'\]/([^<]+)[\$]