|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Difficulty with Regex pattern to validate a stringHi 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?? Nutella wrote:
> I need to validate a string in C-sharp using the Regex class. Try:> > 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_\-]*$" Arne On Jul 8, 8:41 pm, Arne Vajhøj <a***@vajhoej.dk> wrote:
Show quoteHide quote > Nutella wrote: I guess the * means for the whole string? and is the \ needed?> > 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 because the previous reply doesnt have it. On Jul 9, 4:55 pm, Nutella <nutella***@gmail.com> wrote:
> I guess the * means for the whole string? and is the \ needed? It means: Match any number (*) of specified chars ([A-Za-z0-9_\-])> because the previous reply doesnt have it. 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 Hello Julia,
> On Jul 9, 4:55 pm, Nutella <nutella***@gmail.com> wrote: Which isn't needed if you put the '-' at the start or at the end of the list. > >> 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" That's why I omitted it. -- Jesse Houwing jesse.houwing at sogeti.nl 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: Thanks. Also I need the + to ensure I get at least 1 or more, and not> > > 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 an empty string as match e.g. ^[A-Za-z0-9_\-]+$ "Nutella" <nutella***@gmail.com> wrote in message I believe the \ is optional IF the - is either the first or the last 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. character in the list. Someone please correct me if I'm wrong. Nutella wrote:
> On Jul 8, 8:41 pm, Arne Vajhøj <a***@vajhoej.dk> wrote: * means zero to infinite occurrences.>> 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? > and is the \ needed? - has special meaning inside [] as used the first three times.> because the previous reply doesnt have it. 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 Hello Nutella,
> Hi all, ^[A-Za-z0-9_-]*$ > > 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?? should do... -- Jesse Houwing jesse.houwing at sogeti.nl
Other interesting topics
ContextMenu Does Not Work?
Handlling different Data types Replace strings in a text file and get the number of replacements made linq to sql speed question Input - String : Output - Binary Oct Dec Hex Need advice on Class. Thank You Drag Drop - Move Data Between Apps How to use bring to front and Send to back so the rsult is good Show application at Windows Vista Login/Locked screen "on-line" lex and yacc parser generators |
|||||||||||||||||||||||