1

I know there are a lot of these questions here on StackOverflow but i couldn't find exactly what i'am searching for ...

I need a regex that allows letters (including umlauts and others like öäßè), numbers and white space. So no special characters (?!;:#) and no dash (-) or underscore (_)

GDY
  • 2,872
  • 1
  • 24
  • 44

1 Answers1

3

Use \p{L}, a Unicode letter class, to match any letter from any alphabet (i.e. non-ASCII Unicode letters):

^[\d\s\p{L}]+$

Demo: https://regex101.com/r/wfjCjF/3

P.S.

Mind the pattern delimiters when using a regex in preg_match:

preg_match('/^[\d\s\p{L}]+$/', 'öäßè')
            ^              ^
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
  • Sorry iam not very good at regex. On the page you mentioned it looks fine but with preg_match i get Unknown modifier '+' – GDY May 31 '17 at 07:23
  • You have probably forgotten to add pattern delimiters when using the regex in `preg_match()`. The call should look like `preg_match('/(?:\p{L}|\d)+/', ...)`. Mind the slashes there. – Dmitry Egorov May 31 '17 at 07:26
  • @Grandy: I missing spaces in the initial answer. Now updated to support these. Also, all characters classes may be listed in `[...]`, which simplifies the regex. Please see the updated answer. – Dmitry Egorov May 31 '17 at 07:32
  • Ok there was a negation before the function call (!preg_match) ... But it returns alsways 1 if any of the allowed characters is in the string (e.g.: "asdf" but also "asdf#"). How has this to be done to only return true if the string contains only allowed characters ... so if any not allowed character is in it it should return false – GDY May 31 '17 at 07:33
  • Got it: '/^[\d\s\p{L}]+$/' – GDY May 31 '17 at 07:34
  • Maybe consider to also add the complete function call for more unexperienced regex users (like me :P) – GDY May 31 '17 at 07:37
  • Yes, I agree. This may be helpful for others. – Dmitry Egorov May 31 '17 at 07:51
  • What worked for me was this https://stackoverflow.com/a/63658185/251674 – Manuel Alves Oct 08 '21 at 08:13