正则表达式找到内容问题

jef*_*eff 6 regex coldfusion

尝试使用正则表达式refind标记在此示例中使用coldfusion查找括号内的内容

 joe smith <joesmith@domain.com>
Run Code Online (Sandbox Code Playgroud)

结果文本应该是

 joesmith@domain.com
Run Code Online (Sandbox Code Playgroud)

用这个

<cfset reg = refind(
 "/(?<=\<).*?(?=\>)/s","Joe <joe@domain.com>") />
Run Code Online (Sandbox Code Playgroud)

没有运气.有什么建议?

也许是语法问题,它适用于我使用的在线正则表达式测试程序.

Pet*_*ton 9

你不能在CF的正则表达式引擎中使用lookbehind(使用Apache Jakarta ORO).

但是,您可以使用Java的正则表达式,它确实支持它们,并且我创建了一个包装器CFC,使这更容易.可从以下网址获得:http: //www.hybridchill.com/projects/jre-utils.html

(更新:上面提到的包装CFC已经演变成一个完整的项目.有关详细信息,请参阅cfregex.net.)

此外,/.../s内容在此处不是必需/相关的.

所以,从你的例子,但改进的正则表达式:

<cfset jrex = createObject('component','jre-utils').init()/>

<cfset reg = jrex.match( "(?<=<)[^<>]+(?=>)" , "Joe <joe@domain.com>" ) />
Run Code Online (Sandbox Code Playgroud)


快速说明,因为我已经更新了几次正则表达式; 希望它现在处于最佳状态......

(?<=<) # 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.
Run Code Online (Sandbox Code Playgroud)