Home All Groups Group Topic Archive Search About

Difficulty with Regex pattern to validate a string

Author
8 Jul 2009 10:42 PM
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??

Author
9 Jul 2009 12:41 AM
Arne_Vajhøj
Nutella wrote:
> 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
Are all your drivers up to date? click for free checkup

Author
9 Jul 2009 2:55 PM
Nutella
On Jul 8, 8:41 pm, Arne Vajhøj <a***@vajhoej.dk> wrote:
Show quoteHide quote
> Nutella wrote:
> > 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.
Author
9 Jul 2009 3:26 PM
Julia M
On Jul 9, 4:55 pm, Nutella <nutella***@gmail.com> wrote:
> 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
Author
9 Jul 2009 3:59 PM
Jesse Houwing
Hello Julia,

> On Jul 9, 4:55 pm, Nutella <nutella***@gmail.com> wrote:
>
>> 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.

--
Jesse Houwing
jesse.houwing at sogeti.nl
Author
9 Jul 2009 4:04 PM
Nutella
On Jul 9, 11:26 am, Julia M <juliab***@yahoo.com> wrote:
Show quoteHide quote
> On Jul 9, 4:55 pm, Nutella <nutella***@gmail.com> wrote:
>
> > 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_\-]+$
Author
9 Jul 2009 4:06 PM
Jeff Johnson
"Nutella" <nutella***@gmail.com> wrote in message
news:467e1a2c-68d0-4049-bd2f-764673c78bc7@d4g2000yqa.googlegroups.com...

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

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

I believe the \ is optional IF the - is either the first or the last
character in the list. Someone please correct me if I'm wrong.
Author
9 Jul 2009 11:32 PM
Arne_Vajhøj
Nutella wrote:
> On Jul 8, 8:41 pm, Arne Vajhøj <a***@vajhoej.dk> wrote:
>> Nutella wrote:
>>> 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
Author
9 Jul 2009 12:48 AM
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...

--
Jesse Houwing
jesse.houwing at sogeti.nl

Bookmark and Share