如何从`[[:: punct:]]`获取Ruby Regexp标点列表?

Zed*_*TuX 0 ruby regex

我正在使用devise-securitygem并设置它,以要求symbol输入密码(通过config.password_complexity)。

现在,我想显示可能使用的符号。

看看gem的代码,我发现他们实际上是在使用Regexp[[:punct:]]

您能告诉我如何[[:punct:]]从Ruby代码中从POSIX括号表达式中获取符号列表吗?

我期望得到像这样的字符串#$%^*)

Ser*_*sev 7

[[:punct:]]指的是unicode中的标点符号。例如:https : //www.fileformat.info/info/unicode/category/Po/list.htm

s = "foo\u1368bar" # => "foo?bar"
s.split(/[[:punct:]]/) # => ["foo", "bar"]
Run Code Online (Sandbox Code Playgroud)

抱歉,我的问题是使用Ruby获取该列表。

由于缺少更好的主意,您现在总是可以从1循环到unicode中的最大字符数,将其视为字符代码,生成一个1字符的字符串并将其与[[:punct:]]regex 匹配。这是快速而肮脏的实现

punct = 1.upto(65535).map do |x|
  x.chr(Encoding::UTF_8)
rescue RangeError
  nil
end.reject(&:nil?).select do |s|
  s =~ /[[:punct:]]/
end
Run Code Online (Sandbox Code Playgroud)

结果(如我的macOS所示):

Unicode标点