Regex 正则表达式查找内容问题
尝试使用regex refind标记来查找本例中使用coldfusion的括号内的内容Regex 正则表达式查找内容问题,regex,coldfusion,Regex,Coldfusion,尝试使用regex refind标记来查找本例中使用coldfusion的括号内的内容 joe smith <joesmith@domain.com> 用这个 <cfset reg = refind( "/(?<=\<).*?(?=\>)/s","Joe <joe@domain.com>") /> 一点运气都没有。有什么建议吗 可能是语法问题,它在我使用的在线正则表达式测试仪中工作。/\]+)\>$/ /\<([^>]
joe smith <joesmith@domain.com>
用这个
<cfset reg = refind(
"/(?<=\<).*?(?=\>)/s","Joe <joe@domain.com>") />
一点运气都没有。有什么建议吗
可能是语法问题,它在我使用的在线正则表达式测试仪中工作。/\]+)\>$/
/\<([^>]+)\>$/
类似的东西,没有测试过,那是你的;) 我对CF中的正则表达式匹配函数一直不满意。因此,我写了自己的:
<cfscript>
function reFindNoSuck(string pattern, string data, numeric startPos = 1){
var sucky = refindNoCase(pattern, data, startPos, true);
var i = 0;
var awesome = [];
if (not isArray(sucky.len) or arrayLen(sucky.len) eq 0){return [];} //handle no match at all
for(i=1; i<= arrayLen(sucky.len); i++){
//if there's a match with pos 0 & length 0, that means the mime type was not specified
if (sucky.len[i] gt 0 && sucky.pos[i] gt 0){
//don't include the group that matches the entire pattern
var matchBody = mid( data, sucky.pos[i], sucky.len[i]);
if (matchBody neq arguments.data){
arrayAppend( awesome, matchBody );
}
}
}
return awesome;
}
</cfscript>
函数reFindNoSuck(字符串模式、字符串数据、数字startPos=1){
var sucky=refindNoCase(模式、数据、起始时间、真值);
var i=0;
var=[];
if(不是isArray(sucky.len)或arrayLen(sucky.len)eq 0){return[];}//处理一点也不匹配
对于(i=1;i
转储“matches”变量表明它是一个包含2项的数组。第一项是
(因为它匹配整个正则表达式),第二项是joesmith@domain.com
(因为它匹配正则表达式中定义的第一个组——所有后续组也将被捕获并包含在数组中).您不能将lookback与CF的正则表达式引擎(uses)一起使用
但是,您可以使用它,它确实支持它们,我已经创建了一个包装器CFC,使这更容易。可从以下网站获得:
(更新:上面提到的包装CFC已经演变成一个完整的项目。有关详细信息,请参见。)
此外,/../s内容在这里不是必需的/相关的
因此,根据您的示例,但使用改进的正则表达式:
<cfset jrex = createObject('component','jre-utils').init()/>
<cfset reg = jrex.match( "(?<=<)[^<>]+(?=>)" , "Joe <joe@domain.com>" ) />
一个简短的提示,因为我已经更新了几次正则表达式;希望它现在处于最佳状态
(?<=<) # 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.
(?`但不要捕捉它。
你能告诉我们你尝试了什么但没有成功吗?从那开始更容易…你是个天才彼得。这很好用…谢谢你的帮助谢谢亚当,我能使用彼得开发的包装器,但也谢谢你的两分钱。
<cfset jrex = createObject('component','jre-utils').init()/>
<cfset reg = jrex.match( "(?<=<)[^<>]+(?=>)" , "Joe <joe@domain.com>" ) />
(?<=<) # 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.