正则表达式在Coldfusion中匹配整个单词字符串 [英] Regex Match whole word string in coldfusion

查看:68
本文介绍了正则表达式在Coldfusion中匹配整个单词字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个例子

第一个例子

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"); 
} 

示例输出->找到了

第二个例子

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"); 
}

输出->找到了

但是我只想查找整个单词.我遇到的标点符号问题我如何才能将正则表达式用于第二个示例输出:找不到

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

推荐答案

Coldfusion不支持向后搜索,因此,您不能使用真正的零宽度边界"检查.相反,您可以使用分组(幸运的是可以提前使用):

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

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

在这里,(^ | \ W)匹配字符串的开头,并且(?= \ W | $)确保存在一个非字符串-字字符( \ 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 ($).

请参见正则表达式演示

但是,在传递给正则表达式之前,请确保您先对关键字进行了转义.参见 ColdFusion 10现在提供了reEscape()来为本地RE方法准备字符串文字. .

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.

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

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

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

这篇关于正则表达式在Coldfusion中匹配整个单词字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆