Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 27th, 2006, 01:12 PM
Registered User
Join Date: Nov 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regular expressions on C#

I'm trying to check if the string I'm getting from a web form is acceptable but it isn't working at all. I'm using these regular expressions on javascript without any visible problem.

Javascript version:
    var emailFilter1=/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
    var emailFilter2=/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
    var generalFilter=/^[a-zA-Z].[a-zA-Z0-9\-\._ ]+$/
    var generalFilter2=/^[a-zA-Z0-9\-\._ ]+$/

C# version:
Regex filter1 = new Regex(@"/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/");
                Regex filter2 = new Regex(@"/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/");
                Regex generalFilter = new Regex(@"/^[a-zA-Z].[a-zA-Z0-9\-\._ ]+$/");
                Regex generalFilter2 = new Regex(@"/^[a-zA-Z0-9\-\._ ]+$/");

bool isTrue = generalFilter.IsMatch(user.Text);
isTrue is always set at false. I'm stuck on this for hours now. Any help is welcome. :)

Old November 27th, 2006, 01:16 PM
Registered User
Join Date: Nov 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

oh, btw. On the before the last I'm checking if the string begins with a letter and then if it has more any of these: - _ . a-z A-Z 0-9

Last one, I ignore the starting letter and just check for the acceptable chars.

Old November 27th, 2006, 05:08 PM
Authorized User
Join Date: Sep 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts


The first thing I notice when looking at your general filter is that it will NEVER work in C#. You have some mutually-exclusive commands within the pattern. For instance, the first two characters (/^) prevent the pattern from ever matching.

  / => Match a forward slash character.
  ^ => The next character should match the start of the string.

Well, if the first character of the match HAS to be a forward slash, then the second character can NEVER match the start of the string (by definition, it is the second character).

You make the same sort of error with the end of your general filter. You use the pattern ($/).

  $ => Matches the end of the string.
  / => Requires a forward slash AFTER the end of the string.

Without looking too terribly close at your whole filter, you might start by deleting the opening and closing forward slashes, leaving you with a general filter pattern of:

  ^[a-zA-Z].[a-zA-Z0-9\-\._ ]+$

By the way, it is hard to figure out what's wrong with your regular expression patterns when you don't supply any correct sample text strings to test-match with.

- Roger


- Roger Nedel

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Regular Expressions WestRowOps Other Programming Languages 1 May 18th, 2007 05:34 AM
Regular Expressions mega Beginning PHP 1 February 5th, 2007 05:31 PM
I need some help in regular expressions! marwaesmat Perl 2 March 7th, 2006 05:22 PM
regular expressions help kyootepuffy Classic ASP Databases 2 September 10th, 2003 01:37 PM
Regular Expressions Dave Doknjas C# 1 August 9th, 2003 12:05 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.