正则表达式以匹配IRC昵称 [英] Regular expression to match IRC nickname
问题描述
如何使用正则表达式匹配IRC昵称?这样做是否可以在Ruby中完成(使用regex的语法,也许会,但谁知道.)
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.)
IRC昵称可以包含任何字母,数字或以下任何字符:<
- [ ] \
^ { }
An IRC nickname can contain any letter, number, or any of the following characters: <
- [ ] \
^ { }
推荐答案
# 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
以上允许单字符名称.如果需要两个字符,请将*
更改为+
.如果需要三个字符(或更多),请将其更改为{2,}
,其中"2"是最小字符数减去1.
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.
如果最大字符数(例如,EFNet仅允许最多9个字符的昵称,而Freenode最多允许长16个字符的昵称),则可以在逗号后包括该数字(负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
这篇关于正则表达式以匹配IRC昵称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!