Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 27th, 2006, 01:12 PM
Registered User
Join Date: Nov 2006
Location: , , .
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. :)

Reply With Quote
  #2 (permalink)  
Old November 27th, 2006, 01:16 PM
Registered User
Join Date: Nov 2006
Location: , , .
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.

Reply With Quote
  #3 (permalink)  
Old November 27th, 2006, 05:08 PM
Authorized User
Join Date: Sep 2003
Location: Park City, UT, .
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
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 09:24 AM.

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