Regular Expression Apostrophe Problem in .NET

I'm new to using Regular Expressions, but I'm trying to use them for form validation. Everything's going fine so far, except for names that contain an apostrophe (i.e. O'Dell).

This article <!-- m --><a class="postlink" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/paght000001.asp">http://msdn.microsoft.com/library/defau ... 000001.asp</a><!-- m -->, states that this code:
Regex.IsMatch(fname_form, "^[a-zA-Z'.\s]{1,40}$")

"...constrains an input name field to alphabetic characters (lowercase and uppercase), space characters, the single quotation mark (or apostrophe) for names such as O'Dell, and the period or dot character. In addition, the field length is constrained to 40 characters.".

But I'm getting this error when attempting to enter a name with an apostrophe:
Expected token 'eof' found 'NAME'. 'o'-->dell<--'

Huh? If anyone can let me know what I'm doing wrong, that would be great. Thanks.I decided that it would be smart to replace any apostrophe's with a question mark anyway for future use, so I just added a replace function like this:
lname_form = lname_form.replace("'","?")

...and I'll then change it back when displaying the data on a web page or email. Now I'm good-to-go. Thanks:)The period or dot character is a special character in regular expressions so if you want to match the literal you need to escape it. That's a semantic error though not a syntax one so it would result in an actual error message, you'd just get the wrong results.

It looks to me though like the error isn't a regex error at all. It looks to me like the compiler believes the single quote character in the name O'Dell is in fact the closing quote for the string and thus can't figure out what the heck this "Dell" token is outside the quotes.
 
Back
Top