Abubakar
Abubakar

Reputation: 87

Regular expression in c# for input like (integer-anything)

string strRegexclass = @"^([0-9]+)\-([a-zA-Z])$";

I want to make regular expression which accept input like this (1-class). Any integer value before dash(-) and then must have dash then anything after dash.

Upvotes: 1

Views: 53

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626861

If you plan to only match a string that starts with 1 or more ASCII digits, then a hyphen, and then any 0+ chars use:

^[0-9]+-.*$

See the regex demo

Note that \d and [0-9] are not equal in .NET regex flavor.

Upvotes: 0

Ihor Dobrovolskyi
Ihor Dobrovolskyi

Reputation: 1241

You can use the code like this:

string strRegexclass = @"^\d+-.*$";

Or you can use the next code

string strRegexclass = @"^\d+-\w*$";

if you want to allow only letters after the dash.

Upvotes: 1

Related Questions