C# 标记内标记和最后匹配标记的正则表达式匹配
我试图用包含转义字符串的数据解析一些xml标记 一些样品C# 标记内标记和最后匹配标记的正则表达式匹配,c#,regex,C#,Regex,我试图用包含转义字符串的数据解析一些xml标记 一些样品 other tags with our without newlines <tag name="abc1" type="bcd" value="test"><tag name="abc2" type="bcd" value="test"> other tags other tags with our without newlines <tag name="abc2" type="bcd" value="&
other tags with our without newlines
<tag name="abc1" type="bcd" value="test"><tag name="abc2" type="bcd" value="test">
other tags other tags with our without newlines
<tag name="abc2" type="bcd" value="<w:test xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust"><a xmlns:"a:b:c:ddd:">XEduAjr8MoV</a></w:test>">
由于
(?*)
的原因,我在组合多个匹配项时遇到了问题,因为如果两个匹配项都是同一行
有什么办法可以解决这个问题吗?有更好的方法吗?不建议使用正则表达式模式解析xml文件。这是因为xml涉及/需要深度嵌套。不建议使用正则表达式模式解析xml文件。这是因为xml涉及/需要深度嵌套。众所周知,除非没有复杂的标记和奇怪的字符集,否则不应使用regex解析xhtml
但是,如果您想使用正则表达式,对于您的特定示例,您必须使用非贪婪的(或懒惰的)量词:
<tag name="(?<name>\w*?)" type="(?<id>\w*?)" value="(?<value>.*?)">
HERE ---^
also I put it here ---^------------------^
since it is more secure, but it is not needed
这里---^
我也把它放在这里--^--------------------------^
因为它更安全,但它不是必需的
众所周知,不应该使用regex解析xhtml,除非您没有复杂的标记和一组奇怪的字符 但是,如果您想使用正则表达式,对于您的特定示例,您必须使用非贪婪的(或懒惰的)量词:
<tag name="(?<name>\w*?)" type="(?<id>\w*?)" value="(?<value>.*?)">
HERE ---^
also I put it here ---^------------------^
since it is more secure, but it is not needed
这里---^
我也把它放在这里--^--------------------------^
因为它更安全,但它不是必需的
最好不要使用正则表达式解析xml/html。。。使用xslt…最好不要使用正则表达式解析xml/html。。。使用xslt…可能重复的
var regex = new Regex(regstr, RegexOptions.Multiline);
MatchCollection mc = regex.Matches(sourcestring);
<tag name="(?<name>\w*?)" type="(?<id>\w*?)" value="(?<value>.*?)">
HERE ---^
also I put it here ---^------------------^
since it is more secure, but it is not needed