run*_*ode 8 ruby regex syntax brackets
双方括号在正则表达式中意味着什么?我对以下示例感到困惑:
/[[^abc]]/
/[^abc]/
Run Code Online (Sandbox Code Playgroud)
我正在使用Rubular进行测试,但我发现双支架和单支架之间没有任何区别.
Posix字符类使用[:alpha:]符号,在正则表达式中使用,如:
/[[:alpha:][:digit:]]/
Run Code Online (Sandbox Code Playgroud)
您需要在上面的链接中向下滚动查看Posix信息的方法.来自文档:
POSIX括号表达式也类似于字符类.它们提供了上述的便携式替代方案,其附加好处是它们包含非ASCII字符.例如,/\d /仅匹配ASCII十进制数字(0-9); 而/ [[:digit:]] /匹配Unicode Nd类别中的任何字符.
/[[:alnum:]]/ - Alphabetic and numeric character
/[[:alpha:]]/ - Alphabetic character
/[[:blank:]]/ - Space or tab
/[[:cntrl:]]/ - Control character
/[[:digit:]]/ - Digit
/[[:graph:]]/ - Non-blank character (excludes spaces, control characters, and similar)
/[[:lower:]]/ - Lowercase alphabetical character
/[[:print:]]/ - Like [:graph:], but includes the space character
/[[:punct:]]/ - Punctuation character
/[[:space:]]/ - Whitespace character ([:blank:], newline,
carriage return, etc.)
/[[:upper:]]/ - Uppercase alphabetical
/[[:xdigit:]]/ - Digit allowed in a hexadecimal number (i.e., 0-9a-fA-F)
Run Code Online (Sandbox Code Playgroud)
Ruby还支持以下非POSIX字符类:
/[[:word:]]/ - A character in one of the following Unicode general categories Letter, Mark, Number, Connector_Punctuation
/[[:ascii:]]/ - A character in the ASCII character set
# U+06F2 is "EXTENDED ARABIC-INDIC DIGIT TWO"
/[[:digit:]]/.match("\u06F2") #=> #<MatchData "\u{06F2}">
/[[:upper:]][[:lower:]]/.match("Hello") #=> #<MatchData "He">
/[[:xdigit:]][[:xdigit:]]/.match("A6") #=> #<MatchData "A6">
Run Code Online (Sandbox Code Playgroud)