6 Replies Latest reply on Sep 27, 2006 9:54 AM by Newsgroup_User

    SImple regex not working?

    cmschofield
      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. I though the star (*) would solve the problem but I can't seem to get it to work.

      Any help would be greatly appreciated.
        • 1. Re: SImple regex not working?
          Kronin555 Level 1
          Read the quickdocs (especially the first comment).

          http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/functa63.htm#wp1111015

          Basically, if you do an REFind("^$",FORM.middlename) it will return 0 whether or not the string is blank.
          • 2. Re: SImple regex not working?
            Level 7
            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
            zero-length string?

            The problem here is there's no way of distinguishing between "no match" and
            "match at position zero".

            --
            Adam
            • 3. Re: SImple regex not working?
              Level 7
              Java is a bit more clever than CF. See this example:

              <cfset s = "">
              <cfset sRegex = "^[A-Za-z']*$">

              <cfset i = reFind(sRegex, s)>
              <cfoutput>#i#</cfoutput><br />

              <cfset b = s.matches(sRegex)>
              <cfoutput>#b#</cfoutput><br />

              --
              Adam
              • 4. Re: SImple regex not working?
                cmschofield Level 1
                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?
                • 5. Re: SImple regex not working?
                  cmschofield Level 1
                  Thanks Adam. I'll just use the Java method. The more I learn about the CF regular expression methods, the more I hate them.

                  Thank you.
                  • 6. Re: SImple regex not working?
                    Level 7
                    > 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.

                    --
                    Adam