繁体   English   中英

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

[英]How to get the Ruby Regexp punctuation list from the `[[:punct:]]`?

我正在使用devise-security gem,并进行了设置,以便要求密码中有一个symbol (通过config.password_complexity )。

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

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

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

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

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

s = "foo\u1368bar" # => "foo፨bar"
s.split(/[[:punct:]]/) # => ["foo", "bar"]

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

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

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

结果(如我的macOS所示):

Unicode标点

如果您只想要标点符号的示例(因为完整列表相当笨拙),我将选择一个您喜欢的示例,然后确保它确实与模式匹配:

example_punctuation = '(*!@$#jfas,./'.gsub(/[^[:punct:]]/, '')
# "(*!@$#,./"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM