Regex. Digits, Letters and Dashes. what am I doing wrong?

Discussion in 'Microsoft C# .NET' started by Shapper, Aug 30, 2012.

  1. Shapper

    Shapper Guest

    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?

    Thank You,
    Miguel
     
    Shapper, Aug 30, 2012
    #1
    1. Advertisements

  2. Shapper

    Brian Cryer Guest

    "Shapper" <> wrote in message
    news:...
    > 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/regular_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
     
    Brian Cryer, Aug 30, 2012
    #2
    1. Advertisements

  3. Shapper

    Shapper Guest

    On Thursday, August 30, 2012 11:24:52 AM UTC+1, Shapper wrote:
    > 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?
    >
    >
    >
    > Thank You,
    >
    > Miguel


    Hello,

    Thank you for the help. It is working fine.

    And great Regex tester. I am going to use it in the future.

    Thank You,
    Miguel
     
    Shapper, Aug 30, 2012
    #3
  4. On Thu, 30 Aug 2012 11:55:07 +0100, "Brian Cryer"
    <> wrote:

    [snip]

    >Hope this helps.


    Well, it helped ME! Thank you for the good links.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Aug 30, 2012
    #4
  5. Shapper

    Arne Vajhøj Guest

    On 8/30/2012 6:24 AM, Shapper wrote:
    > 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?


    "^[\w-]*$"

    would be my suggestion.

    (prefix with @ or double backslash in C# source code)

    Arne
     
    Arne Vajhøj, Aug 30, 2012
    #5
  6. On 30-08-2012 12:55, Brian Cryer wrote:
    > 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/regular_expression.htm


    Isn't there is a small error in your example for the *? operator?
    You write:

    For example "b[an]*a" when applied to the word "banana" will
    match the entire word, but "b[an]*?a" will match with just "bana".

    .... but AFAICS, "b[an]*?a" will match already at "ba".

    /Anders.
     
    Anders D. Nygaard, Sep 2, 2012
    #6
  7. Shapper

    Brian Cryer Guest

    "Anders D. Nygaard" <> wrote in message
    news:50434b9e$0$291$...
    > On 30-08-2012 12:55, Brian Cryer wrote:
    >> 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/regular_expression.htm

    >
    > Isn't there is a small error in your example for the *? operator?
    > You write:
    >
    > For example "b[an]*a" when applied to the word "banana" will
    > match the entire word, but "b[an]*?a" will match with just "bana".
    >
    > ... but AFAICS, "b[an]*?a" will match already at "ba".


    Yes. You are quite right. Thank you for pointing it out. I'll update it
    today.
    --
    Brian Cryer
    http://www.cryer.co.uk/brian
     
    Brian Cryer, Sep 3, 2012
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianwei Sun

    What's wrong with this Regex.Split

    Jianwei Sun, Apr 13, 2005, in forum: Microsoft C# .NET
    Replies:
    5
    Views:
    143
    Adam Clauss
    Apr 13, 2005
  2. sherifffruitfly
    Replies:
    3
    Views:
    343
    Jon Skeet [C# MVP]
    Feb 11, 2007
  3. DotNetNewbie

    Regex for replacing all consecutive dashes to single dash

    DotNetNewbie, Feb 27, 2008, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    308
    Luc E. Mistiaen
    Feb 27, 2008
  4. DotNetNewbie
    Replies:
    0
    Views:
    149
    DotNetNewbie
    Apr 21, 2008
  5. Ethan Strauss
    Replies:
    4
    Views:
    287
    Martin Honnen
    Jul 11, 2008
Loading...

Share This Page