开发者

How do I match a string that does not contain X with ColdFusion regular expressions?

I asked this question earlier, but it got a negative vote, so I'm rewording it. I have:

<cfset myExpression = "X">
#REFind(myExpression,myString)#

I need to change m开发者_JAVA技巧yExpression so that it returns a value other than zero if there is NOT an X in myString, and a 0 if there is an X in myString.


<cfset string = "abc" />  
<cfoutput>#refind( "^[^X]+$" , string )#</cfoutput> // 1 

<cfset string = "abcX" /> 
<cfoutput>#refind( "^[^X]+$" , string )#</cfoutput> // 0


I am building a validation table

Well, the first thing to check is that you're not re-inventing the wheel - the isValid function can validate a variety of types (creditcard,email,zipcode,etc).

It also provides a way to match against a regex pattern, like this:

<cfif isValid('regex',String,RegexPattern) >

Something to be aware of: the documentation for isValid claims that it uses JavaScript regex, which (if true) is different to the default Apache ORO regex that CF uses for everything else.

For the direct regex version of what you were doing (which does use Apache ORO), you would use:

<cfif refind(RegexPattern,String) >


It's not clear what you're on about with your returnValue bit, though if you're returning a boolean from a function, ditch the cfif and just do one of these instead:

<cfreturn isValid('regex',String,RegexPattern) />

<cfreturn refind(RegexPattern,String) />


if your expression is always a character or set of characters then you want

<cfset myExpression ="[^X]">

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜