Regular expression to match IRC nickname
Asked Answered
G

1

6

How would I use a regular expression to match an IRC nickname? This is being done in Ruby if that makes a difference (it probably will, with the syntax of the regex, but who knows.)

EDIT: An IRC nickname can contain any letter, number, or any of the following characters: < - [ ] \ ^ { }

Gourde answered 2/3, 2011 at 4:4 Comment(6)
Ruby uses PCRE, doesn't it? #3925148Demob
What can and can't be in an IRC nickname? In what positions? What have you tried so far? What didn't work?Neuburger
@codaddict: What do you mean? The rule is to match any IRC nickname.Gourde
@Satya: Not really, only certain characters are allowed in IRC nicknames. Which I edited my answer to include.Gourde
@MartinhoFernandes: I have edited my answer to include this information, and as to what positions can they be in: they can be anywhere. And I really haven't tried anything yet.Gourde
By "any letter", I do you mean "any letter in the Latin alphabet"?Moeller
E
13
# If you are testing a single string
irc_nick_re = /\A[a-z_\-\[\]\\^{}|`][a-z0-9_\-\[\]\\^{}|`]*\z/i 

# If you are scanning them out of a larger string
irc_nick_re = /(?<=[^a-z_\-\[\]\\^{}|`])[a-z_\-\[\]\\^{}|`][a-z0-9_\-\[\]\\^{}|`]*/i 

The above allows single-character names. If two characters are required, change the * to +. If three characters (or more) are required, change it to {2,}, where '2' is the minimum number of characters minus 1.

If there is a maximum number of characters (for example, EFNet only allows nicknames up to 9 characters lone, while Freenode allows nicks up to 16 characters long) then you can include that number (minus 1) after the comma. For example:

# Validate nicknames that are between 3 and 16 characters long (inclusive)
irc_nick_re = /\A[a-z_\-\[\]\\^{}|`][a-z0-9_\-\[\]\\^{}|`]{2,15}\z/i 
Exquisite answered 2/3, 2011 at 4:13 Comment(2)
I don't know of any popular IRC network which allows "<" in nicknames. IRCnet, FreeNode, Quakenet, OFTC, Undernet and EFnet all reject it. Also, you shouldn't match a digit at the beginning of the nickname, since no IRC network allows that either. Furthermore, you're missing "|", "`", and "_", which are allowed. And the "\d" should be "0-9" instead, since those are the only digits allowed, not any Unicode digit.Brewster
@Brewster Great points! I was matching based on the OP's requirements, but not testing against real-world implementations. However, note that \d does not match Unicode digits in Ruby.Exquisite

© 2022 - 2024 — McMap. All rights reserved.