Difficulty with Regex pattern to validate a string

N

Nutella

Hi all,

I need to validate a string in C-sharp using the Regex class.

The rules are:
-can only contain alpha-numeric characters, '_' or '-'
-Nothing else, no spaces, no funny characters.

e.g. "HELLO_WORLD-1234_abc-ABC"

What's the Regex pattern for this??
 
A

Arne Vajhøj

Nutella said:
I need to validate a string in C-sharp using the Regex class.

The rules are:
-can only contain alpha-numeric characters, '_' or '-'
-Nothing else, no spaces, no funny characters.

e.g. "HELLO_WORLD-1234_abc-ABC"

What's the Regex pattern for this??

Try:

@"^[A-Za-z0-9_\-]*$"

Arne
 
J

Jesse Houwing

Hello Nutella,
Hi all,

I need to validate a string in C-sharp using the Regex class.

The rules are:
-can only contain alpha-numeric characters, '_' or '-'
-Nothing else, no spaces, no funny characters.
e.g. "HELLO_WORLD-1234_abc-ABC"

What's the Regex pattern for this??

^[A-Za-z0-9_-]*$

should do...
 
N

Nutella

Nutella said:
I need to validate a string in C-sharp using the Regex class.
The rules are:
-can only contain alpha-numeric characters, '_' or '-'
-Nothing else, no spaces, no funny characters.
e.g.  "HELLO_WORLD-1234_abc-ABC"
What's the Regex pattern for this??

Try:

@"^[A-Za-z0-9_\-]*$"

Arne

I guess the * means for the whole string? and is the \ needed?
because the previous reply doesnt have it.
 
J

Julia M

I guess the * means for the whole string?    and is the \ needed?
because the previous reply doesnt have it.

It means: Match any number (*) of specified chars ([A-Za-z0-9_\-])
from start (^) till end ($)

^[A-Za-z0-9_\-]$ would only match string with a single char. "A"
matches, "AA" doesn't.
\ escapes the - so it can't be mistaken as a list seperator like in "A-
Z"

HTH
 
J

Jesse Houwing

Hello Julia,
I guess the * means for the whole string? and is the \ needed?
because the previous reply doesnt have it.
It means: Match any number (*) of specified chars ([A-Za-z0-9_\-])
from start (^) till end ($)

^[A-Za-z0-9_\-]$ would only match string with a single char. "A"
matches, "AA" doesn't.
\ escapes the - so it can't be mistaken as a list seperator like in
"A-
Z"

Which isn't needed if you put the '-' at the start or at the end of the list.
That's why I omitted it.
 
N

Nutella

I guess the * means for the whole string?    and is the \ needed?
because the previous reply doesnt have it.

It means: Match any number (*) of specified chars ([A-Za-z0-9_\-])
from start (^) till end ($)

^[A-Za-z0-9_\-]$ would only match string with a single char. "A"
matches, "AA" doesn't.
\ escapes the - so it can't be mistaken as a list seperator like in "A-
Z"

HTH


Thanks. Also I need the + to ensure I get at least 1 or more, and not
an empty string as match
e.g. ^[A-Za-z0-9_\-]+$
 
A

Arne Vajhøj

Nutella said:
Nutella said:
I need to validate a string in C-sharp using the Regex class.
The rules are:
-can only contain alpha-numeric characters, '_' or '-'
-Nothing else, no spaces, no funny characters.
e.g. "HELLO_WORLD-1234_abc-ABC"
What's the Regex pattern for this??
Try:

@"^[A-Za-z0-9_\-]*$"

I guess the * means for the whole string?

* means zero to infinite occurrences.
and is the \ needed?
because the previous reply doesnt have it.

- has special meaning inside [] as used the first three times.

Maybe the Regex class is smart enough to see that it is not
needed when the dash is the last before ], but I just always use
it for dash when inside [].

Arne
 
J

Jesse Houwing

Hello Nutella,
I guess the * means for the whole string? and is the \ needed?
because the previous reply doesnt have it.
It means: Match any number (*) of specified chars ([A-Za-z0-9_\-])
from start (^) till end ($)

^[A-Za-z0-9_\-]$ would only match string with a single char. "A"
matches, "AA" doesn't.
\ escapes the - so it can't be mistaken as a list seperator like in
"A-
Z"
HTH
Thanks. Also I need the + to ensure I get at least 1 or more, and not
an empty string as match
e.g. ^[A-Za-z0-9_\-]+$

That should do. Unless you're using it in a ASP.NET RegexValidator, because
that validator will not fire on an empty textbox, in ASP.NET you'll have
to add a RequiredValidator as well.
 
G

Göran Andersson

Nutella said:
Hi all,

I need to validate a string in C-sharp using the Regex class.

The rules are:
-can only contain alpha-numeric characters, '_' or '-'
-Nothing else, no spaces, no funny characters.

e.g. "HELLO_WORLD-1234_abc-ABC"

What's the Regex pattern for this??

If the string may be empty:

^[\w\-]*$

If the string has to contain at least one character:

^[\w\-]+$

^ matches the beginning of the string
$ matches the end of the string
[] is a characrer set matching one character
\w matches A-Z, a-z, 0-9 or _
\- matches the character -
* is a quantifier for zero or more times
+ is a quantifier for one or more times

The escaping backslash before the dash in the set may not be needed, but
a dash has a special meaning inside a character set, and it's clearer
both for the Regex object and for the person reading the code that it's
the character - that is intended. If you later on add more characters,
you could easily forget that it needed escaping:

Adding some characters: ^[\w#!-]+$

Adding some more characters: ^[\w#!-&%]+$

Oops, now you have the span ! to & instead of the three separate
characters...
 
J

Jesse Houwing

Hello Göran,

\w contains a lot more characters in .NET with unicode support enabled. Unless
you specify the EcMaCompliant option, you should not be using this shortcut.

Jesse
Nutella said:
Hi all,

I need to validate a string in C-sharp using the Regex class.

The rules are:
-can only contain alpha-numeric characters, '_' or '-'
-Nothing else, no spaces, no funny characters.
e.g. "HELLO_WORLD-1234_abc-ABC"

What's the Regex pattern for this??
If the string may be empty:

^[\w\-]*$

If the string has to contain at least one character:

^[\w\-]+$

^ matches the beginning of the string
$ matches the end of the string
[] is a characrer set matching one character
\w matches A-Z, a-z, 0-9 or _
\- matches the character -
* is a quantifier for zero or more times
+ is a quantifier for one or more times
The escaping backslash before the dash in the set may not be needed,
but a dash has a special meaning inside a character set, and it's
clearer both for the Regex object and for the person reading the code
that it's the character - that is intended. If you later on add more
characters, you could easily forget that it needed escaping:

Adding some characters: ^[\w#!-]+$

Adding some more characters: ^[\w#!-&%]+$

Oops, now you have the span ! to & instead of the three separate
characters...
 

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

Top