正则表达式麻烦
我一直在努力为我的一个小项目找到一个有效的正则表达式。谁能帮助我使用一个正则表达式来匹配 <>
符号内的任何内容,但前提是它们前面没有 \ 符号?
例如:
<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.
我做错了什么? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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 just
matchbut 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.我会用这个:
I would use this:
试试这个
Try this
您需要的是后视运算符。在这里阅读它们:
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