正则表达式麻烦

发布于 2024-08-02 20:58:22 字数 585 浏览 3 评论 0 原文

我一直在努力为我的一个小项目找到一个有效的正则表达式。谁能帮助我使用一个正则表达式来匹配 <> 符号内的任何内容,但前提是它们前面没有 \ 符号?

例如:

<Escaped characters \<\> are right in the middle of this sentence.>, <Here is another sentence.>

必须匹配

1: Square brackets \<\> are right in the middle of this sentence.
2: here is another sentence.

到目前为止我已经成功了,

/<([^\\][^>]*?)>/ig

但这给出了

1: Escaped characters \<\
2: Here is another sentence.

我做错了什么? :(

I've been struggling with getting a working regular expression for a little project of mine. Can anyone help me with a regex that matches anything inside <> symbols, but only when they are not preceded by a \ symbol?

For example:

<Escaped characters \<\> are right in the middle of this sentence.>, <Here is another sentence.>

Must match

1: Square brackets \<\> are right in the middle of this sentence.
2: here is another sentence.

So far I've managed

/<([^\\][^>]*?)>/ig

but that gives

1: Escaped characters \<\
2: Here is another sentence.

What am I doing wrong? :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

苄①跕圉湢 2024-08-09 20:58:22

Crimson 的答案对我来说在 Regex Powertoy 使用 <转义字符 \ 进行测试时不起作用<\>就在这句话的中间。>,<这是另一个句子。> 作为测试,但这(似乎)有效:

/<(?(?)/gi

给我两个匹配项:
<转义字符\<\>就在这句话的中间。><这是另一句话。>

编辑:我看了一下 Gumbo 说的字符串不匹配。我在 regex.powertoy.org 中匹配它没有任何问题:

替代文本 http: //img362.imageshack.us/img362/3227/regexpowertoyorg.png

在测试中,我确实将原始发布的正则表达式更改为: /(?/gi 效率更高(更少的探测)。

我还注意到在 regex.powertoy.org 的输出中,第四个字符串 (\> 但不是 this\> 看起来很奇怪......打印的替换只是 match 但匹配详细信息清楚地表明匹配是正确的;match\ 但我还注意到第一个和第三个测试字符串替换不打印 " `" 转义尖括号。经过一番(但不是详尽的)尝试后,我认为这是通过 javascript 显示文本的问题,转义的尖括号不会打印转义字符,并且非空尖括号根本不会被打印,所以我认为这个正则表达式可以正常工作。

Crimson's answer is not working for me in testing it in the Regex Powertoy using <Escaped characters \<\> are right in the middle of this sentence.>, <Here is another sentence.> as the test but this (seems) to work:

/<(?<!\\<).*?>(?<!\\>)/gi

Gives me two matches:
<Escaped characters \<\> are right in the middle of this sentence.> and <Here is another sentence.>

Edit: I took a look at the string Gumbo said did not match. I don't have any problems matching it in regex.powertoy.org:

alt text http://img362.imageshack.us/img362/3227/regexpowertoyorg.png

In testing I did change the original posted regex to: /(?<!\\)<(.*?)(?<!\\)>/gi which is more efficient (less probes).

Also I notice in the output of regex.powertoy.org that the forth string (\<hello <match\<this\>> but not this\> looks odd... the printed replacement is justmatchbut the match detail clearly shows that the match is correct;match\. But I also notices that the first and third test string replacements don't print the "`" escaping the angle brackets. After a bit (but not exhaustive) playing around I think that this is an issue with the display of the text via javascript, the escaped angle brackets don't print the escape char, and non-empty angle brackets don't get printed at all. I think this is due to the javascript seeing it as HTML. So; I think this regex is working correctly. But you should test it offline.

南风几经秋 2024-08-09 20:58:22

我会用这个:

/<((?:[^\\>]+|\\.)*)>/

I would use this:

/<((?:[^\\>]+|\\.)*)>/
偏闹i 2024-08-09 20:58:22

试试这个

/<[^\\]([^>]+)>/

Try this

/<[^\\]([^>]+)>/
花桑 2024-08-09 20:58:22

您需要的是后视运算符。在这里阅读它们:

http://www.perl.com /pub/a/2003/07/01/regexps.html

这是您需要的表达式:

/<(?!<\\).*>(?!<\\)/

由于上面的 * 运算符是贪婪的,因此它应该包含任何转义的尖括号 /< >>

编辑:我假设您希望匹配并返回转义尖括号。如果您想要不同的东西,请澄清 - 给出 a) 输入字符串和 b) 要返回的匹配的简洁示例

What you need are look-behind operators. Read about them here:

http://www.perl.com/pub/a/2003/07/01/regexps.html

And here is the expression you need:

/<(?!<\\).*>(?!<\\)/

As the * operator above is greedy, it should include any escaped angle brackets /< />

EDIT: I am assuming that you want escaped angle brackets to be matched and returned. If you want something different, please clarify - give a succinct example of a) the input string and b) the match to be returned

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