ajay_t
ajay_t

Reputation: 2385

Regex for validating string for space at the beginning and end

I am writing regex expression will accept the string those are not starting or ending with spaces. But the spaces in between are allowed. My string contains only alphanumeric character. I have written regex for checking alphanumeric string with specific length as 10 allowing spaces.

[A-Za-z0-9\s]{10}.

This allows spaces at begining and end also. I have tried with \S to ommit space at the begining. But didn't work. Any suggestion on this regex.

Upvotes: 1

Views: 3171

Answers (1)

anubhava
anubhava

Reputation: 785196

You can use this regex:

^[A-Za-z0-9](?!.*?\s$)[A-Za-z0-9\s]{0,9}$

This will enforce 1st and last character to be alphanumeric while allowing space in the middle 8 characters.

Upvotes: 3

Related Questions