JamesW
JamesW

Reputation: 859

Regular expression for a list of numbers without other characters

I need a regular expression for a list of numbers in a text box with carriage returns and line feeds, but no other characters.

e.g.

1234 
5678 
5874 
3478

I have this:

Regex(@"\d\r\n${0,}?");

... but it is accepting commas when I paste them into my text box:

e.g.

1234,
5678
5874,
3478

Can someone please tell me what I'm doing wrong?

Thanks

Upvotes: 1

Views: 752

Answers (2)

Try * instead of {0,} - they mean the same thing.

Upvotes: 0

Jens
Jens

Reputation: 25563

Quite a lot is wrong with your Regex =)

  • \d matches one number, not several
  • $ matches the end of the line, {0,} means, that the symbol before it may appear zero or more times. But zero or more end of lines are not very useful.
  • The ? is superfluous, I think.
  • You are missing the start of line character.

Your regex matches the example you have given, because it matches the "8" in the second line.

Use this regex instead:

"^(\d*\r\n)*$"

Upvotes: 2

Related Questions