0

I need a RegEx to match the following condition:

  • 1 or 2 words
  • Total minimum length is 3
  • Total maximum length is 50
  • Accept accented characters
  • No numbers is a plus

Examples of accepted strings:

  • Karmen Garcia
  • João Pedro
  • Vitor
  • maxiliamum LóPezz
  • Nadál
  • loriPsium PsisVõèrbizbbbbbbbbbb

Examples of non accepted string:

  • Jony Cam Vanis
  • 9lucas P.us-kas1 Li1

What I tried:

  • ^(?!.*?\s{2})[A-Za-z ]{3,50}$
  • ^([a-zA-Z][a-zA-Z]){3,50}$
Fábio Silva
  • 129
  • 2
  • 14

3 Answers3

1

Here you go:

(?=^.{3,50}$)(\s*\p{L}+\s*){1,2}

Or if you don't want any leading whitespace, trailing whitespace, or more than one space between the words:

(?=^.{3,50}$)\p{L}+(\s\p{L}+)?

EDIT:

As this other SO post shows, Javascript has a problem with Unicode character classes. So, \p{L} won't work in Javascript.

What does that mean for you? Well, that other post shows three different solutions. Which solution is right for you depends on whether or not you know in advance exactly which accented characters or non-word (e.g. punctuation) characters might entered.

One possible approach is to list out the valid accented characters then concatenate it in to the regex:

var validWordCharacters = "[" + "a-z" + "A-Z" + "àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇߨøÅ寿œ" + "]";

var regex = "(?=^.{3,50}$)" + validWordCharacters + "+(\s" + validWordCharacters + "+)?";

regexCompiled = new RegExp(regex);

Another possible (and more concise) solution is to use a range of code points:

var validWordCharacters = "[" + "a-z" + "A-Z" + "\u00C0-\u024F" + "]";

var regex = "(?=^.{3,50}$)" + validWordCharacters + "+(\s" + validWordCharacters + "+)?";

regexCompiled = new RegExp(regex);
Community
  • 1
  • 1
Travis
  • 2,135
  • 17
  • 29
1

You failed to tag what regex flavor/programming language/tool. But here's one way of doing it:

^(?=.{3,50}$)\p{L}*(\s\p{L}*)?$

It uses a positive look-ahead to make sure it's between 3 and 50 characters. Follwing that it simple checks for unicode letter class characters, optionally followed by a space and more letters, i.e. one or two words.

See it here at regex101.

Edit

OK - so for javascript you could try

^(?=.{3,50}$)[^\s\d]*(\s[^\s\d]*)?$

It's basically the same, only instead of matching the unicode class, it matches non space and digits. That's not perfect, but a simple solution that'll work in most (latin) cases.

See this here at regex101.

SamWhan
  • 8,296
  • 1
  • 18
  • 45
0

I think you may use next. It check minimum length but not check maximum. You may ensure it in different check

^([A-Za-z]{3,50}|([A-Za-z]+\s[A-Za-z]+))$
Michael Piankov
  • 1,989
  • 1
  • 8
  • 19
  • Well thats fine. Do you know how to add accented characters? 'Joao Pedro' is OK but 'João Pedro' is not passing. Thank you – Fábio Silva Jan 13 '17 at 14:55
  • does not meet the request, you could have a word of 1 char with a second of 2 that is good and dont pass or second word of 150 char withfirst of 10 that is bad but pass – NeronLeVelu Jan 13 '17 at 14:56
  • @FábioSilva well in truth it you may add the first and the last character as A-Z. I'm sorry | i haven't your alphabet and I just copy some letters from post (ã-õ). – Michael Piankov Jan 13 '17 at 15:00
  • @NeronLeVelu, thank you for your comment. But it not clear. In my two words first with 1 char and next with 2 (`'a bc'`) will pass with right part of regex `([A-Za-z]+\s[A-Za-z]+)`. And two words for 161 chars are passed to and I write that I dont check the whole length in that case. Am I ask for your question? – Michael Piankov Jan 13 '17 at 15:06