Regular expression to match IRC nickname

前端 未结 1 1576
时光说笑
时光说笑 2021-02-09 13:48

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 kno

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-09 14:18

    # 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 
    

    0 讨论(0)
提交回复
热议问题