Not to belabor the obvious, but is $username set to anything? Print it out before the regexp loop to make sure. Nothing in the code you've shown us so far sets it.
(Update - yeh, what Broquaint said... an hour ago.)
Comment on Re: Pattern matching against a variable