Reputation: 2887
Im having following reg-ex which is working OK, the only thing is if user type any value which is valid and press than spaces I got error, how can I avoid the space at the end of the values?
@"^[a-z\d][\da-z-.]*[a-z\s\d]$",
Upvotes: 1
Views: 80
Reputation: 1186
^[a-z\d][\da-z-.]*[a-z\s\d][\s]*$
Use this if you want to accept any amount of whitespace characters at the end (small S
).
^[a-z\d][\da-z-.]*[a-z\s\d][\S]*$
Use this if you don't want to accept strings with white spaces at the end (capital S
).
Or you can also use the Trim
function after you matched it with the unchanged regex.
Upvotes: 1
Reputation: 1099
simply using
String.Trim();
string foo = " hello ";
string bar = foo.Trim();
Console.WriteLine(bar); // writes "hello"
See more:How to remove all white spaces from the start or end of a string?
Upvotes: 0