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

  • Thread starter Thread starter Shapper
  • Start date Start date
S

Shapper

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 said:
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.
 
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
 
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
 
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 said:
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.
 
Back
Top