The regex expression (\w+)? means "capture one or more word characters... or don't". This is the same as saying (\w*): "capture zero or more word characters", and if zero characters are captured, you are left with an empty – but perfectly defined – string.