1

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]$", 
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88

2 Answers2

1
^[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.

rdonatoiop
  • 1,185
  • 1
  • 14
  • 28
Bedford
  • 1,136
  • 2
  • 12
  • 36
0

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?

Community
  • 1
  • 1
Poomrokc The 3years
  • 1,099
  • 2
  • 10
  • 23