"Shapper" <(E-Mail Removed)> wrote in message
news:49c0227f-beaf-4f58-9ed4-(E-Mail Removed)...
> Hello,
>
> I need to use a Regex to check if a String is a match where:
> 1 - Allow Digits, Letters and Dashes (-);
> 2 - Allow the string to be empty.
>
> I have the following: "^\w+-$"
>
> But this is not working. What am I missing?
Whilst \w will match against any lphanumeric character, the + means you want
one or more. So for zero or more alphanumeric characters you would use:
^\w*$
now for the dash, firstly I'd normally escape a dash although depending on
the context it might not need it, but to play safe: \- Next, you want either
an alphanumeric or a dash, so you'll need a group, so I think what you are
after is:
^(\w|\-)*$
That should be it.
A couple of tips:
1. Whilst there are a number of regex resources on the net, the
"cheat-sheet" which I regularly refer to is:
http://www.cryer.co.uk/glossary/r/re...expression.htm
2. When developing and testing regex expressions I find it useful to use a
standalone regex. So whilst it lacks some refinements I developed the
following site as a single regex tester a few years back:
http://regex.cryer.info/ it uses the ASP.NET regex engine, so should behave
the same way as the regex you are using.
Hope this helps.
--
Brian Cryer
http://www.cryer.co.uk/brian