perhaps ^[0-9a-zA-Z\-öÖäÄüÜ\s]*
I would not worry about space at either end as it can be trimmed ( if not done already)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey guys,
I'm really not that great when it comes to regex. However, I need a regular expression to ensure, a username matches a given pattern. Allowed are: 0-9, a-Z, öÖ äÄ üÜ ß, one whitespace/ space between two character but none at the beginning and none at the end, "-" signs also not at the beginning and the end but between characters.
Right now, this is what I made:
return Validator::make($data, [
'user' => ['required', 'string', 'max:100', 'unique:user','regex'=>'^[0-9a-zA-Z-öÖäÄüÜ ]*'],
]);
Regex can be found here: https://www.phpliveregex.com/p/FHE
Would be so happy for any help 🙏🏻
@Mikegk I don’t think that can be done in a single regex offhand – at least not in a way I can think of.
But you can use not-regex to add a negative constraint to the existing positive constraint:
return Validator::make($data, [
'user' => [
'required',
'string',
'max:100',
'unique:user',
'regex' => '^(?:[0-9a-zA-ZäÄöÖüÜß]+(?: |-*)?)+[0-9a-zA-ZäÄöÖüÜß]$',
'not_regex' => '^\d+$'
]
]);
Please or to participate in this conversation.