pcre expression for irc nicknames?
Asked Answered
C

2

1

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

Chenault answered 13/10, 2010 at 14:17 Comment(0)
U
2

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.

Ury answered 12/4, 2014 at 10:24 Comment(0)
K
0

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
Komsa answered 13/10, 2010 at 14:23 Comment(3)
I was trying to replace the character(s) matched with "". Sorry, forgot to mention that. The provided does not seem to work, but I have gotten down to something: "#([^a-z0-9]{0,32})#i" -- it may not do exactly what I need, but I'm using a substr() to continuously remove the first character (if it's a number) until it's a letter, symbol, or blank. If you have any other suggestions, let me know. And thanks for replying/helping :)Chenault
If you just want to delete invalid characters, use preg_replace('/[^a-z0-9.-]+/i', '', $content). You shouldn't be trying to validate and fix the format in the same operation.Komsa
According to this question the character set you have above is too limiting for IRC nicknames.Indelible

© 2022 - 2024 — McMap. All rights reserved.