Search code examples
phpregexirc

IRC Hostmask matching


For my IRC bot I need to match a hostmask with another hostmask with wildcards in it.

Example:

Pablo!Pablo@Pablo.users should match: Pa?lo!P?blo@??blo.users

The questionmark means every character and number.

If it's possible then it would be nice to see it match the string below:

Pablo!Pablo@Pablo.users matches with Pablo!Pablo@Pab*

The ! and @ are always in the hostmask.

Could someone make a regular expression for this task?

I've tried, succeeded but the code is huge and often inaccurate.

Thanks allot!


Solution

  • I wrote this for my PHPIRCd I'm creating.

    function match($mask, $expression) {
        $expression = str_replace('\\*', '.+', preg_quote($expression, '/'));
        return preg_match('/^' . $expression. '$/', $mask);
    }
    

    You could call it like so: match('Pablo!Pablo@Pablo', 'Pab*o!*@*blo'). This may not be exactly what you're looking for, but I hope it sends you on the right track.