Solved

Regular Expression Question

Posted on 2006-07-11
6
392 Views
Last Modified: 2008-02-01
I am using the following function to test for valid alpha-numeric input:

public bool IsAlphaNumeric(String strToCheck)
{
    Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");

      return !objAlphaNumericPattern.IsMatch(strToCheck);
 }


The only problem with the expression is that it doesn't allow spaces between the words. Is there a way I can modify this expression to allow spaces?

Thanks!
0
Comment
Question by:marmaxx
6 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 300 total points
ID: 17079388
Just use the expression:

[^a-zA-Z0-9 ]

I.e. add a space at the end.
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 17079754

/^[a-zA-Z0-9]+$/
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17080116
try:

^[\w\d\s]+$

and don't negatate the returned bool:

public bool IsAlphaNumeric(String strToCheck)
{
      Regex objAlphaNumericPattern = new Regex("^[\w\d\s]+$");
      return objAlphaNumericPattern.IsMatch(strToCheck);
 }


note:
\w is short hand for [A-Za-z]
\d ' ' [0-9]
\s ' ' white space
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17080125
sorry typo... by negatate i mean negate
0
 
LVL 6

Expert Comment

by:MuhammadAdil
ID: 17080596
Hi Dear

Check this

 public bool IsAlphaNumeric(String strToCheck)
        {

            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9 ]");

            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 17080626
@MuhammadAdil: I already wrote this answer.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now