正则表达式过滤具有特定属性的 XAML 标签

发布于 2024-12-04 22:55:43 字数 275 浏览 1 评论 0原文

我有以下与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子是时光的心 2024-12-11 22:55:43

正则表达式

/<[^>]*Content="foo".*?>/g

这意味着启动正则表达式 (/) 并搜索 < 后跟零个或多个而不 > ([^>]*) 后跟 Content="foo" 后跟零个或多个任何内容 (.*) un -贪婪地,后跟“>”并结束正则表达式 /

测试代码:

用javascipt编写,但可以轻松移植到其他语言。

x = ['<Button x:Name="Button1" /><Button x:Name="Button2" Content="foo" /><Button x:Name="Button1" /><Button Content="foo" />']

console.log( x[0].match(/<[^>]*Content="foo".*?>/g) )

更新为匹配完全相反的(根据OP的要求)

正则表达式

使用否定先行断言,如详细信息此处

<((?!Content="foo")[^>])*>

这意味着匹配 < 后跟零个或多个非 > ([^>]*) 每个前面都没有 Content="foo" ,后跟 >。添加括号用于必要的分组。

但请记住 - 使用正则表达式代替 XML 解析会让堆栈溢出的人对你非常生气......所以最好匿名发布这样的问题。 ;)

Regex

/<[^>]*Content="foo".*?>/g

Which means start regex (/) and search for < folled by zero or more not >s ([^>]*) followed by Content="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.

x = ['<Button x:Name="Button1" /><Button x:Name="Button2" Content="foo" /><Button x:Name="Button1" /><Button Content="foo" />']

console.log( x[0].match(/<[^>]*Content="foo".*?>/g) )

Updated to match exact opposite (as required by OP)

Regex

Using negative lookahead assertion, as detailed here

<((?!Content="foo")[^>])*>

Which means match < followed by zero or more not >s ([^>]*) that each do not have Content="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. ;)

傾旎 2024-12-11 22:55:43
<([^<>](?!\sContent\s*=\s*("foo"|'foo')))*>

这...但是请不要使用正则表达式来解析 XML。

我们使用 [^<>] 捕获的每个字符都会检查它后面是否有一个空格和 Content="foo"。如果为 true,我们会失败,因此捕获会回滚一个字符,并且正则表达式会检查终止 >

http://regexr.com?2umr9

您必须单击一下,以便“激活”正则表达式。

一些小注意事项:这个正则表达式是错误的,因为它试图解析 XML,但是忽略这一点,它不会被 SContent="foo" (忽略)或 Content='foo' 愚弄(捕获)或 :Content='foo'(忽略)或 Content = "foo"(捕获)。如果您使用将 \s 视为与 XML 相同的空格字符列表的正则表达式解析器,这一点就很明显了:-) 否则,一些“奇怪的、外来的”空格可能会稍微破坏它。请记住将其与区分大小写的解析一起使用!

<([^<>](?!\sContent\s*=\s*("foo"|'foo')))*>

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 the Content="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) or Content='foo' (catched) or :Content='foo' (ignored) or Content = "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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文