Darren Darren - 1 year ago 57
C# Question

.Net Multiline Regex expression to restrict to integers

I am having a problem with a very simple regular expression.

I want to restrict the entry in a multi-line TextBox to just integers. The regular expression I have works fine in single line mode (for a single line TextBox, not using multiline option), but allows alpha characters to creep in when in multiline mode, but only once a new line has been entered.

My code (C#) is something like:

Regex regExpr = new Regex("^(\d*)$", RegexOptions.Multiline)
return regExpr.IsMatch(testString);


I want the following examples to be valid:

1

1\\n

1\\n2\\n3


I want the following to be invalid

A

A1\\n2

1\\n2\\nA3


Thanks in advance.

Answer Source

What about

(\d?\\n*)?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download