Regular expression

T

Tony Johansson

Hello!

I want to use regular expression to define an Id with the following format.
It's an Id number for an animal which should start with a capital letter,
followed by unknown number of letters and spaces, and then end with at least
one digit.
This regular expression seems to work when I have made some testing but is
it correct.

bool status = Regex.IsMatch(input, @"^[A-Z]+[a-zA-Z ]*\d+$");

//Tony
 
A

Arne Vajhøj

I want to use regular expression to define an Id with the following format.
It's an Id number for an animal which should start with a capital letter,
followed by unknown number of letters and spaces, and then end with at least
one digit.
This regular expression seems to work when I have made some testing but is
it correct.

bool status = Regex.IsMatch(input, @"^[A-Z]+[a-zA-Z ]*\d+$");

It looks OK to me.

I would probably use @"^[A-Z][a-zA-Z ]*\d+$" because uppercase
letters after the first is already covered by the next.

Two small notes:
1) I will suggest that you write unit tests for this type of stuff.
It is pretty easy to cover this with 10-20 test cases.
2) I am not sure that it is a good idea with id's with such
a loose syntax.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
I want to use regular expression to define an Id with the following
format.
It's an Id number for an animal which should start with a capital letter,
followed by unknown number of letters and spaces, and then end with at
least
one digit.
This regular expression seems to work when I have made some testing but
is
it correct.

bool status = Regex.IsMatch(input, @"^[A-Z]+[a-zA-Z ]*\d+$");

It looks OK to me.

I would probably use @"^[A-Z][a-zA-Z ]*\d+$" because uppercase
letters after the first is already covered by the next.

Two small notes:
1) I will suggest that you write unit tests for this type of stuff.
It is pretty easy to cover this with 10-20 test cases.
2) I am not sure that it is a good idea with id's with such
a loose syntax.

Arne

It's just for learning regular expression.

//Tony
 
A

Arne Vajhøj

Arne Vajhøj said:
I want to use regular expression to define an Id with the following
format.
It's an Id number for an animal which should start with a capital letter,
followed by unknown number of letters and spaces, and then end with at
least
one digit.
This regular expression seems to work when I have made some testing but
is
it correct.

bool status = Regex.IsMatch(input, @"^[A-Z]+[a-zA-Z ]*\d+$");

It looks OK to me.

I would probably use @"^[A-Z][a-zA-Z ]*\d+$" because uppercase
letters after the first is already covered by the next.

Two small notes:
1) I will suggest that you write unit tests for this type of stuff.
It is pretty easy to cover this with 10-20 test cases.
2) I am not sure that it is a good idea with id's with such
a loose syntax.

It's just for learning regular expression.

It is also good to learn unit tests!

:)

Arne
 
A

Anders Eriksson

I want to use regular expression to define an Id with the following format.
It's an Id number for an animal which should start with a capital letter,
followed by unknown number of letters and spaces, and then end with at least
one digit.
This regular expression seems to work when I have made some testing but is
it correct.

bool status = Regex.IsMatch(input, @"^[A-Z]+[a-zA-Z ]*\d+$");

Hello Tony,

I just wanted to recommend a tool for testing regular expressions. It's
called Expresso and made by http://www.ultrapico.com/

It's free!

And it makes it "easy" to test your expressions.

// Anders
 

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

Similar Threads

regular expression 4
regular expression 7
why does this regular expression match 18
Regular expression 4
Regular Expression Help 2
Regular Expression Help 1
regular expressions 5
regular expression NxM 9

Top