• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

SImple regex not working?

Explorer ,
Sep 27, 2006 Sep 27, 2006

Copy link to clipboard

Copied

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.
TOPICS
Advanced techniques

Views

525

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 27, 2006 Sep 27, 2006
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

Votes

Translate

Translate
Advocate ,
Sep 27, 2006 Sep 27, 2006

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 27, 2006 Sep 27, 2006

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 27, 2006 Sep 27, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 27, 2006 Sep 27, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 27, 2006 Sep 27, 2006

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 27, 2006 Sep 27, 2006

Copy link to clipboard

Copied

LATEST
> 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation