pcre expression for irc nicknames?

被刻印的时光 ゝ 提交于 2019-12-08 08:32:22

问题


Hey guys, I'm having a few issues with using PCRE to make a irc nickname format correctly. I'm not good with PCRE, and I'd love some suggestions from those of you who do use PCRE / regex. :)

I'm currently using this expression: /^([^A-Za-z]{1})([^A-Za-z0-9-.]{0,32})$/ I'm using it as such: preg_replace($regex, $replaceWith, $content)

I assumed this meant, starting from the front to the end, any characters that are not A-Z, a-z, or 0-9 for the first character, replace it. Any characters after that, in which are not A-Z a-z, 0-9, -, or ., replace it.

If anyone could help, you would be helping out greatly. It's the only thing stopping me from releasing a chat product to a new forum software. :/


回答1:


I've been using the following regex to check for nicknames in my IRC logs:

/<([a-zA-Z\[\]\\`_\^\{\|\}][a-zA-Z0-9\[\]\\`_\^\{\|\}-]{1,31})>/

using it in a preg_match like so:

preg_match('/<([a-zA-Z\[\]\\`_\^\{\|\}][a-zA-Z0-9\[\]\\`_\^\{\|\}-]{1,31})>/', $line)

I simply check whether a user said something on the line and the line wasn't just a join/part message or nick change or something of the sort, but it would be easy to put it into a preg_replace too.

It matches the nicks according to the nickname rules in RFC 2812 Section 2.3.1, which state that the first character must be a letter (a-zA-Z) or special ([]{}^`|_\) and the rest of the characters may be letters, special, digits (0-9) or hyphens (-). I chose the max length of 32 based on GTAnet's NICKLEN=32 instead of the RFC's max length of 9, because many networks don't seem to follow this standard. The max length varies between different IRC networks so tweak it as required.




回答2:


I'm not sure what you're trying to replace with, but it'd be better to check if the string matches a username (instead of not matching) and then replace if it doesn't:

$regex = '/^[a-z][a-z0-9.-]{0,32}$/i';
if (!preg_match($regex, $content))
{
  // do your replace here
}

The regular expression says:

^                   # Beginning of string
  [a-z]             # Match a single a-z
  [a-z0-9.-]{0,32}  # Match between 0 and 32 occurances of a-z, 0-9, . or -
$                   # End of string
/i                  # Make the pattern case-insensitive


来源:https://stackoverflow.com/questions/3924648/pcre-expression-for-irc-nicknames

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!