Search code examples
phpregexnetbios

Regex for netbios names


I got this issue figuring out how to build a regexp for verifying a netbios name. According to the ms standard these characters are illegal \/:*?"<>|

So, thats what I'm trying to detect. My regex is looking like this

^[\\\/:\*\?"\<\>\|]$

But, that wont work.

Can anyone point me in the right direction? (not regexlib.com please...) And if it matters, I'm using php with preg_match.

Thanks


Solution

  • Your regular expression has two problems:

    1. you insist that the match should span the entire string. As Andrzej says, you are only matching strings of length 1.
    2. you are quoting too many characters. In a character class (i.e. []), you only need to quote characters that are special within character classes, i.e. hyphen, square bracket, backslash.

    The following call works for me:

    preg_match('/[\\/:*?"<>|]/', "foo");  /* gives 0: does not include invalid characters */
    preg_match('/[\\/:*?"<>|]/', "f<oo"); /* gives 1: does include invalid characters */