正则表达式过滤具有特定属性的 XAML 标签
我有以下与 XAML 标签匹配的正则表达式:
<[^<>]*>
这将返回这两行:
<Button x:Name="Button1" />
<Button x:Name="Button2" Content="foo" />
我想要做的是过滤掉其中包含“Content =“foo””的标签。
那里有类似的例子,但在这种情况下,引号让我绊倒。
有什么想法吗?
I have the following Regex that matches XAML tags:
<[^<>]*>
This would return both of these lines:
<Button x:Name="Button1" />
<Button x:Name="Button2" Content="foo" />
What I want to do is filter out tags that have "Content="foo"" in them.
There are similar examples out there, but in this case the quotation marks are tripping me up.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正则表达式
这意味着启动正则表达式 (
/
) 并搜索<
后跟零个或多个而不>
([^>]*
) 后跟Content="foo"
后跟零个或多个任何内容 (.*
) un -贪婪地,后跟“>”并结束正则表达式/
。测试代码:
用javascipt编写,但可以轻松移植到其他语言。
更新为匹配完全相反的(根据OP的要求)
正则表达式
使用否定先行断言,如详细信息此处
这意味着匹配
<
后跟零个或多个非>
([^>]*
) 每个前面都没有Content="foo"
,后跟>
。添加括号用于必要的分组。但请记住 - 使用正则表达式代替 XML 解析会让堆栈溢出的人对你非常生气......所以最好匿名发布这样的问题。 ;)
Regex
Which means start regex (
/
) and search for<
folled by zero or more not>
s ([^>]*
) followed byContent="foo"
followed by zero or more anythings (.*
) un-greedily, followed by '>' and end regex/
.Test Code:
Written in javascipt, but can be ported into other languages easily.
Updated to match exact opposite (as required by OP)
Regex
Using negative lookahead assertion, as detailed here
Which means match
<
followed by zero or more not>
s ([^>]*
) that each do not haveContent="foo"
in front of them followed by>
. Brackets added for necessary grouping.But remember - using regex as a substitute for XML parsing will make people on stack overflow get really angry with you... so better to post questions like this anonymously. ;)
这...但是请不要使用正则表达式来解析 XML。
我们使用
[^<>]
捕获的每个字符都会检查它后面是否有一个空格和Content="foo"
。如果为 true,我们会失败,因此捕获会回滚一个字符,并且正则表达式会检查终止>
。http://regexr.com?2umr9
您必须单击一下,以便“激活”正则表达式。
一些小注意事项:这个正则表达式是错误的,因为它试图解析 XML,但是忽略这一点,它不会被
SContent="foo"
(忽略)或Content='foo' 愚弄
(捕获)或:Content='foo'
(忽略)或Content = "foo"
(捕获)。如果您使用将\s
视为与 XML 相同的空格字符列表的正则表达式解析器,这一点就很明显了:-) 否则,一些“奇怪的、外来的”空格可能会稍微破坏它。请记住将其与区分大小写的解析一起使用!This... But PLEASE, don't use Regexes to parse XML.
Each character we capture with
[^<>]
we check if it is followed by one space and theContent="foo"
. If true we fail so the capture is rollbacked of one character and the Regex checks for the terminating>
.http://regexr.com?2umr9
You have to click around a little so that the Regex is "activated".
Some small notes: this Regex is WRONG because it's trying to parse XML, but ignoring this, it won't be fooled by
SContent="foo"
(ignored) orContent='foo'
(catched) or:Content='foo'
(ignored) orContent = "foo"
(catched). This clearly if you are using a Regex parser that treats\s
as the same list of space characters as XML :-) Otherwise some "strange, alien" spaces could break it a little. Remember to use it with case sensitive parsing!