By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

hyphen in regular expression (cSharp)

P: n/a
Input
a-

Patterns

\w*-*
\w*(-*)
\w*(-)*
\w*[-]*

No of the patterns results in a match. Tried escaps(\) before hyphen
still no match.

My requirement is to make sure a string contains only digits, alphabets
and hypen (each 0 or more times).

Oct 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Well, you're not being very specific about your requirements, but the
following would work with them as you have stated them:

[\w-]*

Translated: Match any combination of 0 or more of digits, alphabetical
characters, or hyphens.

--
HTH,

Kevin Spencer
Microsoft MVP
Computer Control Freak
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

<ja************@yahoo.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Input
a-

Patterns

\w*-*
\w*(-*)
\w*(-)*
\w*[-]*

No of the patterns results in a match. Tried escaps(\) before hyphen
still no match.

My requirement is to make sure a string contains only digits, alphabets
and hypen (each 0 or more times).

Oct 9 '06 #2

P: n/a

I would think all of those patterns result in matches. Could it be some
mistake in your code?

ja************@yahoo.com wrote:
Input
a-

Patterns

\w*-*
\w*(-*)
\w*(-)*
\w*[-]*

No of the patterns results in a match. Tried escaps(\) before hyphen
still no match.

My requirement is to make sure a string contains only digits, alphabets
and hypen (each 0 or more times).
Oct 9 '06 #3

P: n/a
The patterns would certainly match "a-" but not the requirements he posted.
It is a matter of sequence. The regular expression:

\w*-*

literally means "a match is zero or more word characters *followed by* zero
or more hyphen characters. So, the string "-a" would result in 2 matches,
one for the "-" and one for the "a", while the regular expression:

[\w-]*

literally means "a match is any combination 0 or more of word characters
and/or hypens, so that the string "-a" would result in a single match
containing both characters.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"Truong Hong Thi" <th*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>
I would think all of those patterns result in matches. Could it be some
mistake in your code?

ja************@yahoo.com wrote:
>Input
a-

Patterns

\w*-*
\w*(-*)
\w*(-)*
\w*[-]*

No of the patterns results in a match. Tried escaps(\) before hyphen
still no match.

My requirement is to make sure a string contains only digits, alphabets
and hypen (each 0 or more times).

Oct 9 '06 #4

P: n/a
My requirement is for validation and it should allow only digits,
alphabets and hyphens in the text the user enters.

Thanks

Kevin Spencer wrote:
The patterns would certainly match "a-" but not the requirements he posted.
It is a matter of sequence. The regular expression:

\w*-*

literally means "a match is zero or more word characters *followed by* zero
or more hyphen characters. So, the string "-a" would result in 2 matches,
one for the "-" and one for the "a", while the regular expression:

[\w-]*

literally means "a match is any combination 0 or more of word characters
and/or hypens, so that the string "-a" would result in a single match
containing both characters.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"Truong Hong Thi" <th*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...

I would think all of those patterns result in matches. Could it be some
mistake in your code?

ja************@yahoo.com wrote:
Input
a-

Patterns

\w*-*
\w*(-*)
\w*(-)*
\w*[-]*

No of the patterns results in a match. Tried escaps(\) before hyphen
still no match.

My requirement is to make sure a string contains only digits, alphabets
and hypen (each 0 or more times).
Oct 9 '06 #5

P: n/a
I answered your question. See below.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

<ja************@yahoo.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
My requirement is for validation and it should allow only digits,
alphabets and hyphens in the text the user enters.

Thanks

Kevin Spencer wrote:
>The patterns would certainly match "a-" but not the requirements he
posted.
It is a matter of sequence. The regular expression:

\w*-*

literally means "a match is zero or more word characters *followed by*
zero
or more hyphen characters. So, the string "-a" would result in 2 matches,
one for the "-" and one for the "a", while the regular expression:

[\w-]*

literally means "a match is any combination 0 or more of word characters
and/or hypens, so that the string "-a" would result in a single match
containing both characters.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"Truong Hong Thi" <th*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googleg roups.com...
>
I would think all of those patterns result in matches. Could it be some
mistake in your code?

ja************@yahoo.com wrote:
Input
a-

Patterns

\w*-*
\w*(-*)
\w*(-)*
\w*[-]*

No of the patterns results in a match. Tried escaps(\) before hyphen
still no match.

My requirement is to make sure a string contains only digits,
alphabets
and hypen (each 0 or more times).

Oct 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.