views:

60

answers:

4

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 myExpression 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.

+1  A: 

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) />
Peter Boughton
+2  A: 
<cfset string = "abc" />  
<cfoutput>#refind( "^[^X]+$" , string )#</cfoutput> // 1 

<cfset string = "abcX" /> 
<cfoutput>#refind( "^[^X]+$" , string )#</cfoutput> // 0
Bradley Moore
IF myString contains "ABC", then the expression returns a value other than zero.IF myString contains "ABCX", then the expression returns 0.
cf_PhillipSenn
<cfset string = "abc" /><cfoutput>#refind( "^[^X]+$" , string )#</cfoutput> // 1<cfset string = "abcX" /><cfoutput>#refind( "^[^X]+$" , string )#</cfoutput> // 0
Bradley Moore
By Jove, I think that's it! I'm looking up what the $ means and will accept this answer in a sec.
cf_PhillipSenn
I'm not sure I understand what the +$ means.
cf_PhillipSenn
@cf_PhillipSenn : The + means one or more of the previous construct. In this case, it means one or more "not-X". The $ is the end of the string, similar to ^ being the beginning.
Ben Doom
Keep in mind that "^[^ben]+$" is not the same thing as not finding "ben". [] indicates you are using a class, not a literal string. Running the former against "psenn" returns 0, because the 'e's match.
Ben Doom
Oh. That's a great example. I'll have to remember that if more than one character is being matched.
cf_PhillipSenn
Worth pointing out that this answer (`^[^X]+$`) requires at least one character - i.e. an empty string will also return false. Use `^[^X]*$` if you want to allow empty string.
Peter Boughton
+1  A: 

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

<cfset myExpression ="[^X]">

BioBuckyBall
Thanks Lucas! That's what I thought too! But [^X] returns the first position of a character that is not X.
cf_PhillipSenn