By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,673 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,673 IT Pros & Developers. It's quick & easy.

Regex help

P: n/a
I've been trying to get this going for awhile now, and need help. I've
done a regex object, and when I use IsMatch, it's behavior is quite weird.

I am trying to use Regex to make sure that a variable is only
alphanumeric (no strange characters).

Here's the code:

Regex regExp = new Regex("[azAZ09_]*");

// note: _username is a HtmlInputText
if ( !(regExp.IsMatch(_username.Value)) )
{
//should only go in here if _username contains non-alphanumeric
characters
}

Even if I put strange characters, such as []%^$#!@#, it never goes in
that if block. I've tried getting rid of the ! such as

if (regExp.IsMatch(_username.Value)) { //code }

and it always goes in this if block no matter what I input, wether it
contains only alphanumeric or non-alphanumeric.

Any ideas?

Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
The problem that you're seeing is because you're not constraining where the
regex can match. If you have something like:

####A#####

it will match the "A" part (though since it's a * match, it doesn't even
have to match one character to succeed).

If you add the ^ and $ anchors to your regex, it should work as you expect:
Regex regExp = new Regex("^[azAZ09_]*$");
--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided AS IS with no warranties, and confers no rights.
"Bibe" <m_*******@hotmail.com> wrote in message
news:Bo******************@news02.bloor.is.net.cabl e.rogers.com... I've been trying to get this going for awhile now, and need help. I've
done a regex object, and when I use IsMatch, it's behavior is quite weird.

I am trying to use Regex to make sure that a variable is only
alphanumeric (no strange characters).

Here's the code:

Regex regExp = new Regex("[azAZ09_]*");

// note: _username is a HtmlInputText
if ( !(regExp.IsMatch(_username.Value)) )
{
//should only go in here if _username contains non-alphanumeric
characters
}

Even if I put strange characters, such as []%^$#!@#, it never goes in
that if block. I've tried getting rid of the ! such as

if (regExp.IsMatch(_username.Value)) { //code }

and it always goes in this if block no matter what I input, wether it
contains only alphanumeric or non-alphanumeric.

Any ideas?

Nov 15 '05 #2

P: n/a
The problem that you're seeing is because you're not constraining where the
regex can match. If you have something like:

####A#####

it will match the "A" part (though since it's a * match, it doesn't even
have to match one character to succeed).

If you add the ^ and $ anchors to your regex, it should work as you expect:
Regex regExp = new Regex("^[azAZ09_]*$");
--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided AS IS with no warranties, and confers no rights.
"Bibe" <m_*******@hotmail.com> wrote in message
news:Bo******************@news02.bloor.is.net.cabl e.rogers.com... I've been trying to get this going for awhile now, and need help. I've
done a regex object, and when I use IsMatch, it's behavior is quite weird.

I am trying to use Regex to make sure that a variable is only
alphanumeric (no strange characters).

Here's the code:

Regex regExp = new Regex("[azAZ09_]*");

// note: _username is a HtmlInputText
if ( !(regExp.IsMatch(_username.Value)) )
{
//should only go in here if _username contains non-alphanumeric
characters
}

Even if I put strange characters, such as []%^$#!@#, it never goes in
that if block. I've tried getting rid of the ! such as

if (regExp.IsMatch(_username.Value)) { //code }

and it always goes in this if block no matter what I input, wether it
contains only alphanumeric or non-alphanumeric.

Any ideas?

Nov 15 '05 #3

P: n/a
>
If you add the ^ and $ anchors to your regex, it should work as you expect:
> Regex regExp = new Regex("^[a-zA-Z0-9_]*$");


or use + instead of * to avoid empty strings:

Regex regExp = new Regex("^[a-zA-Z0-9_]+$");
if (regExp.IsMatch(""))
{
Console.WriteLine("Matched empty string");
}
Nov 15 '05 #4

P: n/a
>
If you add the ^ and $ anchors to your regex, it should work as you expect:
> Regex regExp = new Regex("^[a-zA-Z0-9_]*$");


or use + instead of * to avoid empty strings:

Regex regExp = new Regex("^[a-zA-Z0-9_]+$");
if (regExp.IsMatch(""))
{
Console.WriteLine("Matched empty string");
}
Nov 15 '05 #5

P: n/a
I misread the OP. Nevermind my post.
"Michael Mayer" <mr*****@charter.net> wrote in message
news:eI**************@tk2msftngp13.phx.gbl...

If you add the ^ and $ anchors to your regex, it should work as
you expect:
> Regex regExp = new Regex("^[a-zA-Z0-9_]*$");


or use + instead of * to avoid empty strings:

Regex regExp = new Regex("^[a-zA-Z0-9_]+$");
if (regExp.IsMatch(""))
{
Console.WriteLine("Matched empty string");
}

Nov 15 '05 #6

P: n/a
I misread the OP. Nevermind my post.
"Michael Mayer" <mr*****@charter.net> wrote in message
news:eI**************@tk2msftngp13.phx.gbl...

If you add the ^ and $ anchors to your regex, it should work as
you expect:
> Regex regExp = new Regex("^[a-zA-Z0-9_]*$");


or use + instead of * to avoid empty strings:

Regex regExp = new Regex("^[a-zA-Z0-9_]+$");
if (regExp.IsMatch(""))
{
Console.WriteLine("Matched empty string");
}

Nov 15 '05 #7

P: n/a
Thanks alot to all of you that replied. Tu-Thach solution works great
for me, because any character that is non-numerical will be a match, and
I understand now what I was doing wrong from what Eric said.

I also tried Eric's way of doing it, and it dosen't seem to work. Like I
said, I do have a solution to my problem, but I'm trying to see why
Eric's solution isn't working. From what I understand, the anchors ^
and $ are saying that it should match anywhere from the end of the
string to the end of the string. Exactly what is this going to do for
me? Isn't it doing that anyway? Sorry if my questions seem stupid.

Here's what I was doing with Eric's way:

I added the not (^) so that any character that is not alphanumerical
would be a match. It also always goes it in the if block now.

Regex regExp = new Regex("^[^azAZ09_]*$");
if (regExp.IsMatch(_username.Value))
{
// Error here.
}

Cheers.

Eric Gunnerson [MS] wrote:
The problem that you're seeing is because you're not constraining where the
regex can match. If you have something like:

####A#####

it will match the "A" part (though since it's a * match, it doesn't even
have to match one character to succeed).

If you add the ^ and $ anchors to your regex, it should work as you expect:
> Regex regExp = new Regex("^[azAZ09_]*$");


Nov 15 '05 #8

P: n/a
Thanks alot to all of you that replied. Tu-Thach solution works great
for me, because any character that is non-numerical will be a match, and
I understand now what I was doing wrong from what Eric said.

I also tried Eric's way of doing it, and it dosen't seem to work. Like I
said, I do have a solution to my problem, but I'm trying to see why
Eric's solution isn't working. From what I understand, the anchors ^
and $ are saying that it should match anywhere from the end of the
string to the end of the string. Exactly what is this going to do for
me? Isn't it doing that anyway? Sorry if my questions seem stupid.

Here's what I was doing with Eric's way:

I added the not (^) so that any character that is not alphanumerical
would be a match. It also always goes it in the if block now.

Regex regExp = new Regex("^[^azAZ09_]*$");
if (regExp.IsMatch(_username.Value))
{
// Error here.
}

Cheers.

Eric Gunnerson [MS] wrote:
The problem that you're seeing is because you're not constraining where the
regex can match. If you have something like:

####A#####

it will match the "A" part (though since it's a * match, it doesn't even
have to match one character to succeed).

If you add the ^ and $ anchors to your regex, it should work as you expect:
> Regex regExp = new Regex("^[azAZ09_]*$");


Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.