473,490 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regex help

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?

Nov 15 '05 #1
8 5512
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" <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("[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?

Nov 15 '05 #2
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" <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("[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?

Nov 15 '05 #3
>
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
>
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
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
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
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.

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


Nov 15 '05 #8
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.

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


Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4773
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
20
8016
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
17
3940
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
7
2217
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim...
9
2070
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
4
2620
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
7
2562
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
4187
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
1
12128
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
0
1611
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.