hyphen in regular expression (cSharp)

  • Thread starter Thread starter jayanthigk2004
  • Start date Start date
J

jayanthigk2004

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).
 
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.
 
I would think all of those patterns result in matches. Could it be some
mistake in your code?
 
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 said:
I would think all of those patterns result in matches. Could it be some
mistake in your code?

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).
 
My requirement is for validation and it should allow only digits,
alphabets and hyphens in the text the user enters.

Thanks

Kevin said:
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 said:
I would think all of those patterns result in matches. Could it be some
mistake in your code?

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

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

Thanks

Kevin said:
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 said:
I would think all of those patterns result in matches. Could it be some
mistake in your code?

(e-mail address removed) 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).
 
Back
Top