473,414 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

hyphen in regular expression (cSharp)

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
5 8725
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lonelyplanet999 | last post by:
Hi, I'm studying perl programming now. Previously I encountered a script with 3 statements as below. # space between a +, - or " char b4 a keyword? remove it! $in{keywords}=~s/ \+ +/ \+/g;...
9
by: Mark Downes | last post by:
I need to split a string based on a character, such as a space (0x20). The caveat is that I need to ignore the character if it is found between a pair of some other characters, such as...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
3
by: Henrik Dahl | last post by:
Hello! The regular expression matches on a, b or c. How to put a hyphen itself as either a or c, i.e. something like (from hyphen to c)? Best regards, Henrik Dahl
4
by: Daniel | last post by:
I would like to get some help on regular expression to validate decimal numbers. It should also match negative value, empty value. The expression:...
7
by: Tizzah | last post by:
What is wrong with that? regex = /^(http|https):\/\/+({1}+)*\.{2,5}(({1,5})?\/.*)?$/ if(field.hpage.value != regex.test(field.hpage.value)){ alert("Bad Homepage") field.hpage.focus()...
7
by: Anat | last post by:
Hi, What regex do I need to split a string, using javascript's split method, into words-array? Splitting accroding to whitespaces only is not enough, I need to split according to whitespace,...
9
by: Mark Rae | last post by:
Hi, This time, I'm looking for a regular expression which says "the string must contain exactly seven or exactly eight digits" e.g. 123456 fails 1234567 passes 12345678 passes 123456789...
4
by: Peter | last post by:
Hi all, I am searching through directories trying to find the prefix to a number of files. Unfortunately the files don't have a standard naming convention yet. So some of them appear as:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.