Regex help

B

Bibe

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("[a–zA–Z0–9_]*");

// 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?
 
E

Eric Gunnerson [MS]

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("^[a–zA–Z0–9_]*$");

--
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 said:
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("[a–zA–Z0–9_]*");

// 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?
 
M

Michael Mayer

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");
}
 
B

Bibe

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("^[^a–zA–Z0–9_]*$");
if (regExp.IsMatch(_username.Value))
{
// Error here.
}

Cheers.
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("^[a–zA–Z0–9_]*$");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Regex to test Alpha characters 4
Regex OnKeyDown question 1
Regex: replacing \n and spaces 4
Regex help 2
Help me with regex! 5
Regex Question 3
vb.net regex question 4
Need a regex for this... 3

Top