正则表达式在coldfusion中匹配整个单词串

时间:2021-11-04 19:26:21

Im trying this example

我试着这个例子

first example

keyword = "star"; 
myString = "The dog sniffed at the star fish and growled";
regEx = "\b"& keyword &"\b"; 
if (reFindNoCase(regEx, myString)) { 
     writeOutput("found it"); 
} else { 
     writeOutput("did not find it"); 
} 

Example output -> found it

示例输出 - >找到它

second example

keyword = "star"; 
myString = "The dog sniffed at the .star fish and growled";
regEx = "\b"& keyword &"\b"; 
if (reFindNoCase(regEx, myString)) { 
     writeOutput("found it"); 
} else { 
     writeOutput("did not find it"); 
}

output -> found it

输出 - >找到它

but i want to find only whole word. punctuation issue for me how can i using regex for second example output: did not find it

但我想找到整个单词。标点问题对我来说如何使用正则表达式进行第二个示例输出:没找到它

1 个解决方案

#1


5  

Coldfusion does not support lookbehind, so, you cannot use a real "zero-width boundary" check. Instead, you can use groupings (and fortunately a lookahead):

Coldfusion不支持lookbehind,因此,您不能使用真正的“零宽度边界”检查。相反,您可以使用分组(幸运的是前瞻):

regEx = "(^|\W)"& keyword &"(?=\W|$)";

Here, (^|\W) matches either the start of a string, and (?=\W|$) makes sure there is either a non-word character (\W) or the end of string ($).

这里,(^ | \ W)匹配字符串的开头,(?= \ W | $)确保存在非单词字符(\ W)或字符串结尾($)。

See the regex demo

请参阅正则表达式演示

However, make sure you escape your keyword before passing to the regex. See ColdFusion 10 now provides reEscape() to prepare string literals for native RE-methods.

但是,请确保在传递给正则表达式之前转义关键字。请参阅ColdFusion 10现在提供reEscape()来为本机RE方法准备字符串文字。

Another way is to match spaces or start/end of string:

另一种方法是匹配空格或字符串的开头/结尾:

<cfset regEx = "(^|\s)" & TABLE_NAME & "($|\s)">

#1


5  

Coldfusion does not support lookbehind, so, you cannot use a real "zero-width boundary" check. Instead, you can use groupings (and fortunately a lookahead):

Coldfusion不支持lookbehind,因此,您不能使用真正的“零宽度边界”检查。相反,您可以使用分组(幸运的是前瞻):

regEx = "(^|\W)"& keyword &"(?=\W|$)";

Here, (^|\W) matches either the start of a string, and (?=\W|$) makes sure there is either a non-word character (\W) or the end of string ($).

这里,(^ | \ W)匹配字符串的开头,(?= \ W | $)确保存在非单词字符(\ W)或字符串结尾($)。

See the regex demo

请参阅正则表达式演示

However, make sure you escape your keyword before passing to the regex. See ColdFusion 10 now provides reEscape() to prepare string literals for native RE-methods.

但是,请确保在传递给正则表达式之前转义关键字。请参阅ColdFusion 10现在提供reEscape()来为本机RE方法准备字符串文字。

Another way is to match spaces or start/end of string:

另一种方法是匹配空格或字符串的开头/结尾:

<cfset regEx = "(^|\s)" & TABLE_NAME & "($|\s)">