-1

I have really looked for an answer to my problem but I can't seem to find it. So forgive me if I create a duplicate here.

My issue is that I'm creating a script and I need to validate a username and I realize that this has to be done using a regular expression; which I suck in.

  • Can contain any character; even local like russian, chinese, greek, swedish etc.
  • Can contain underscores, hyphens and dots.
  • Can contain numbers
  • Can contain only one single whitespace.

What usernames can not contain, is the following:

! " # ¤ % & / ( ) = ? ` \ ´ ' ^ ~ ¨ § | @ £ $ € { [ ] }`

So how in the world one do create a regex based on this is beyond my skills. I haven't found a single question/answer that has the same requirements. I would be over-happy for help on creating this regex, guys.

5
  • See "Write a title that summarizes the specific problem" in How do I ask a good question?.
    – Oriol
    Jan 2, 2015 at 0:32
  • I'm sorry, but I didn't find a good way of formatting the heading. But I will change it.
    – FredoSk
    Jan 2, 2015 at 0:37
  • 1
    this has to be done using a regular expression - why? if you have a blacklist of characters you can just check for each with strstr.
    – Halcyon
    Jan 2, 2015 at 0:44
  • Halcyon: I just thought that a regex was the best solution...(?)
    – FredoSk
    Jan 2, 2015 at 0:50
  • @FredoSk - Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    – Mr. Llama
    Jan 2, 2015 at 1:44

3 Answers 3

1

Try to use:

^(?:[\x{2D}\x{2E}\x{5F}\p{L}\p{N}]+)(?: (?:[\x{2D}\x{2E}\x{5F}\p{L}\p{N}]+))$

\x{2D} ... Match the character -
\x{2E} ... Match the character .
\x{5F} ... Match the character _
\p{L} ... Match any Unicode Letter.
\p{N} ... Match any Unicode Number.


if (preg_match('/^(?:[\x{2D}\x{2E}\x{5F}\p{L}\p{N}]+)(?: (?:[\x{2D}\x{2E}\x{5F}\p{L}\p{N}]+))$/usim', $subject)) {
    # Successful match
} else {
    # Match attempt failed
}
0
0
$pattern = '/[^\!\¤\%\&\/\(\)\=\?\`\\\^\~\¨\§\|\@\£\$\€\{\[\]\}\"\']+/';
preg_match($pattern, "test test test");

Should do the trick. This creates actually a white list. It checks the string for all but these characters, but the ^ is responsible for this. It should return 1 when the string has normal characters and 0 when the string contains one of the black listed characters.

To check for whitespaces do an extra preg_match.

$pattern = '/^[^ ]* [^ ]*$/';
preg_match($pattern, "test test test");  //should return 0.
1
  • Thanks. Still, it gives me a code error. Here's the code with your regex implemented: if(preg_match(/[^\!\"\#\¤\%\&\/()\=\?`\\\´\'\^\~\¨\§\|\@\£\$\€\{[]\}]/g, $username)) { echo 'bla bla bla'; exit; } I guess the quoting gives the error
    – FredoSk
    Jan 2, 2015 at 0:38
0

You can do it in a two step, check for any matches with this preg_match_all and preg_quote(RegEx):
(Will return number of matches)

$pattern = preg_quote("!\"#¤%&()=?`´'^~¨§|@£$€{[]}]");
preg_match_all("/[$pattern]/", $password);

And then count spaces in the password: substr-count

int substr_count ($password , " ");

Putting it all together

$pattern = preg_quote("!\"#¤%&()=?`´'^~¨§|@£$€{[]}]","/");
if (!preg_match_all("/[$pattern]/", $password) and (substr_count ($password , " ") < 2)) {
    //Pasword is OK
    echo "Works!\n";
} else {
    //Password has issues
    echo "Does not work :( \n";
}

Or using the more effective strpbrk (returns the string after the first character match from the pattern):

$pattern = "!\"#¤%&()=?`´'^~¨§|@£$€{[]}]";
if (!strpbrk($password,$pattern) and (substr_count ($password , " ") < 2)) {
    //Pasword is OK
    echo "Works!\n";
} else {
    //Password has issues
    echo "Does not work :( \n";
}   

Good luck from Richard, Bergen :D

8
  • Hey, Richard! Thanks, I will try your suggestion now and come back :)
    – FredoSk
    Jan 2, 2015 at 0:41
  • Parse error: syntax error, unexpected '/' in...
    – FredoSk
    Jan 2, 2015 at 0:42
  • add ' to the regex string :/
    – Richard87
    Jan 2, 2015 at 0:43
  • Sory way to late for this, slight change in the code, added " instead of ' and a \" in the code :)
    – Richard87
    Jan 2, 2015 at 0:45
  • Gives me: Notice: Undefined variable: € in... Warning: preg_match(): Unknown modifier '(' in
    – FredoSk
    Jan 2, 2015 at 0:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.