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: < - [ ] \ ^ { }

C. Ross
  • 31,137
  • 42
  • 147
  • 238
Mark Szymanski
  • 56,590
  • 24
  • 70
  • 87

1 Answers1

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 
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 3
    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. – Hinrik Apr 27 '11 at 22:45
  • @Hinrik 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](http://stackoverflow.com/questions/6998713/scanning-for-unicode-numbers-in-a-string-with-d). – Phrogz Aug 10 '11 at 02:59