我從來沒有高興的正則表達式匹配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>
適用於您的問題,這是我的例子:
<cfset origString = "joe smith <[email protected]>" />
<cfset regex = "<([^>]+)>" />
<cfset matches = reFindNoSuck(regex, origString) />
傾銷的「匹配」變量表明,它是與2項的數組。第一個將是<[email protected]>
(因爲它匹配整個正則表達式),第二個將是[email protected]
(因爲它匹配正則表達式中定義的第一個組 - 所有後續組也將被捕獲幷包含在數組中)。
你如何告訴我們你曾經嘗試過,但沒有工作?從那裏開始更容易... – jhwist 2010-06-15 15:07:40