正则表达式失败,我不明白为什么

I think I've been looking at this too long. I can't seem to get this regex to work.

Code:

$pattern='/([0-9A-Z\-])*(#)(\s*)/i'; 

if (preg_match($pattern,'B-25-1abc-SW-19# ',$matches) )  {
    echo $matches[0];
}   

(from the comments):
I was expecting it to print out the B-25-1abc-SW-19# text, but its not printing anything because its not getting into the true part of the if statement.

I also tried changing the pattern to look like:

$pattern='/^$([0-9A-Z\-])*(#)(\s*)/i'; 

But that hasn't fixed it either.

You have probably misplaced your quantifier (although it should still match the whole string):

$pattern='/([0-9A-Z\-])*(#)(\s*)/i'; 

should be:

$pattern='/([0-9A-Z\-]*)(#)(\s*)/i';

If it really is just matches[0] you are after, you should check your original string, perhaps you have some utf8 character for your - that is not the minus-character you have in your pattern (or the other way around).

The star was after the first ) instead of ] :)

            $pattern='/([0-9A-Z\-]*)(#)(\s*)/i'; 

            $matches= array();
            if (preg_match($pattern,'B-25-1abc-SW-19# ',$matches) )  {
               print_r($matches);
            }   

//N.B.
//http://php.net/manual/en/function.preg-match.php
//If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on. 

I just as well tried it on phpfiddle, it does return me the results. I have also made a few editings to your regex.

http://phpfiddle.org/main/code/3x6-6w2

Perhaps you have an issue with the string you're working on?