正则表达式查找内容问题 [英] regex find content question
问题描述
尝试使用regex refind标记在此示例中使用coldfusion查找括号内的内容
Trying to use regex refind tag to find the content within the brackets in this example using coldfusion
joe smith <joesmith@domain.com>
生成的文本应为
joesmith@domain.com
使用此
<cfset reg = refind(
"/(?<=\<).*?(?=\>)/s","Joe <joe@domain.com>") />
没有运气。任何建议?
也许是一个语法问题,它在我使用的在线正则表达式测试工具。
Maybe a syntax issue, it works in an online regex tester I use.
推荐答案
您不能在CF的正则表达式引擎中使用lookbehind(使用 Apache Jakarta ORO < a>)。
You can't use lookbehind with CF's regex engine (uses Apache Jakarta ORO).
但是,您可以使用 Java的正则表达式,但它支持他们,我创建了一个包装CFC,使这更容易。可用范围:
http://www.hybridchill.com/projects/jre -utils.html
However, you can use Java's regex though, which does support them, and I've created a wrapper CFC that makes this even easier. Available from: http://www.hybridchill.com/projects/jre-utils.html
(更新:上述包装程序CFC已演变为完整项目,请参阅 cfregex.net 。)
(Update: The wrapper CFC mentioned above has evolved into a full project. See cfregex.net for details.)
,/.../s这里不需要/相关的东西。
Also, the /.../s stuff isn't required/relevant here.
所以,从你的例子,但改进regex:
So, from your example, but with improved regex:
<cfset jrex = createObject('component','jre-utils').init()/>
<cfset reg = jrex.match( "(?<=<)[^<>]+(?=>)" , "Joe <joe@domain.com>" ) />
快速注意,因为我更新了正则表达式次;希望它现在是最好的...
A quick note, since I've updated that regex a few times; hopefully it's at its best now...
(?<=<) # positive lookbehind - start matching at `<` but don't capture it.
[^<>]+ # any char except `<` or `>`, the `+` meaning one-or-more greedy.
(?=>) # positive lookahead - only succeed if there's a `>` but don't capture it.
这篇关于正则表达式查找内容问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!