irc 昵称的 PCRE 表达式?

发布于 2024-09-27 10:45:20 字数 383 浏览 1 评论 0原文

大家好,我在使用 PCRE 正确制作 irc 昵称格式时遇到了一些问题。我不擅长 PCRE,我希望使用 PCRE / 正则表达式的人提供一些建议。 :)

我目前正在使用以下表达式: /^([^A-Za-z]{1})([^A-Za-z0-9-.]{0,32})$/< /代码> 我这样使用它: preg_replace($regex, $replaceWith, $content)

我认为这意味着,从头到尾,任何不是 AZ、az 或 0 的字符-9为第一个字符,替换它。此后的任何字符(其中不是 AZ az、0-9、- 或 .)都将替换它。

如果有人可以提供帮助,那么您将提供很大的帮助。这是阻止我向新论坛软件发布聊天产品的唯一原因。 :/

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. :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑,眼淚并存 2024-10-04 10:45:20

我一直在使用以下正则表达式来检查 IRC 日志中的昵称:

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

在 preg_match 中使用它,如下所示:

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

我只是检查用户是否在线路上说了些什么,并且该线路不仅仅是加入/部分消息或昵称更改或类似的东西,但也很容易将其放入 preg_replace 中。

它根据 RFC 2812 第 2.3.1 节中的昵称规则匹配昵称,其中规定第一个字符必须是字母 (a-zA-Z) 或特殊字符 ([]{}^`|_\),并且其余字符可以是字母、特殊字符、数字 (0-9) 或连字符 (-)。我根据 GTAnet 的 NICKLEN=32 选择最大长度 32,而不是 RFC 的最大长度 9,因为许多网络似乎不遵循此标准。不同 IRC 网络之间的最大长度有所不同,因此请根据需要进行调整。

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.

小耗子 2024-10-04 10:45:20

我不确定您要替换什么,但最好检查字符串是否与用户名匹配(而不是不匹配),如果不匹配则进行替换:

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

正则表达式表示:

^                   # 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文