This content has been marked as final. Show 6 replies
Read the quickdocs (especially the first comment).
Basically, if you do an REFind("^$",FORM.middlename) it will return 0 whether or not the string is blank.
On Wed, 27 Sep 2006 15:45:09 +0000 (UTC), cmschofield wrote:
> I have a simple regular expression that validates a middle name:
> <cfset valid = REFind("^[A-Za-z' ]*$",FORM.middlename)>
> I want the middle name to be optional, but when I try a blank entry in the
> form, the REFind returns 0.
Right. REFind() returns the character position at which the match starts,
right? So which character position would the match start at in a
The problem here is there's no way of distinguishing between "no match" and
"match at position zero".
Java is a bit more clever than CF. See this example:
<cfset s = "">
<cfset sRegex = "^[A-Za-z']*$">
<cfset i = reFind(sRegex, s)>
<cfset b = s.matches(sRegex)>
Theoretically, I've made the expression optional by adding the (*) for zero or many occurances of the character set [A-Za-z' ]. Have I got this wrong?
Thanks Adam. I'll just use the Java method. The more I learn about the CF regular expression methods, the more I hate them.
> Thanks Adam. I'll just use the Java method. The more I learn about the CF regular expression methods, the more I hate them.
I think they're fine (and much better in CFMX than in older versions).
REFIND() does exactly what it claims it does, and indeed was giving you the
correct answer, remember ;-)
CF and Java obviously use the same regex processor, so they have the same
shortcomings as far as what regex "grammar" they understand. For example
Perl's regexes do a whole bunch of stuff that neither Java nor CF can do,
and I presume PHP is the same.
I guess there are probably third-party Java classes out there that cover
more options, having said that (I've never had need to investigate yet).
I'd probably approach your situation in two steps:
<cfif not len(x) or reFind(regex,s)>
It's clearer in intend, for one thing.