Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex that checks that a string should not start or end with a space and should not end with a dot (.)

Tags:

regex

space

As per the requirement I need to generate a regex to match a String that doesn't start or end with space. Apart from that the string should not end with a special character dot(.). As per my understanding I've generated a regex "\\S(.*\\S)?$" which restrict the string that has a space at the beginning and at the end of the string. With this expression I need to validate the regex for string that ends with dot. Any sort of help would be appreciated.

like image 239
Anooj Agarwal Avatar asked Oct 17 '25 02:10

Anooj Agarwal


1 Answers

Use following regex

^\S.*[^.\s]$

Regex explanation here

Regular expression visualization


If you want to match single character then you can use look-ahead and look behind-assertion.

^(?=\S).+(?<=[^.\s])$

Regex explanation here

Regular expression visualization


If look-behind not supports then use

^(?=\S).*[^.\s]$

Regex explanation here

Regular expression visualization

like image 106
Pranav C Balan Avatar answered Oct 18 '25 14:10

Pranav C Balan