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

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
 
B

Brian Cryer

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.
 
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

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
 
G

Gene Wirchenko

On Thu, 30 Aug 2012 11:55:07 +0100, "Brian Cryer"

[snip]
Hope this helps.

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

Sincerely,

Gene Wirchenko
 
A

Arne Vajhøj

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
 
A

Anders D. Nygaard

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.
 
B

Brian Cryer

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top