Link to home
Start Free TrialLog in
Avatar of Pete
PeteFlag for United Kingdom of Great Britain and Northern Ireland

asked on

regex expression

Hello, I want to only allow an input if it satisfies the following, can you please advise on a suitable regex expression.

Less than or equal to 8 characters
Does not have a preceding or following empty space
Is only made up of a-z, A-Z, 0-9 or a hyphen -


clear, rather than concise preferred...

Thanks
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Would you allow leading / trailing blanks, and just trim them off, or would you like to reject inputs with any empty space?

You might enjoy this article that shows how we go about building regular expressions, and testing them for success:
https://www.experts-exchange.com/articles/7830/A-Quick-Tour-of-Test-Driven-Development.html
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pete

ASKER

Thanks I would just stop the script with an error until the leading blank spaces could be sorted.

oBda doesnt mention the blank space, is that dealt with the expression anyway?
'\A[a-zA-Z0-9-]{1,8}\Z'
Avatar of oBdA
oBdA

Yes, since the space isn't in the set of allowed characters.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pete

ASKER

Is the /A /Z syntax compatible with powershell, rather than ^ and $?

Thanks
Avatar of Pete

ASKER

how about allowing white space anywhere other than at the start or beginning?

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial