ruby - 通过给定的正则表达式生成字符串

标签 ruby regex

如何通过 ruby​​ 中给定的正则表达式生成字符串?

Class MyRegex
   def self.generate_string(reg)
    #return generated string.
   end
end

当我打电话时

MyRegex.generate_string(/a*/) #it will return random string.

预期输出:

aaa
aa
aaaaa

等等

最佳答案

来晚了一点,但我已经创建了一个强大的 ruby gem ,它解决了最初的问题:

https://github.com/tom-lord/regexp-examples

/this|is|awesome/.examples #=> ['this', 'is', 'awesome']
/https?:\/\/(www\.)?github\.com/.examples #=> ['http://github.com', 'http://www.github.com', 'https://github.com', 'https://www.github.com']

关于ruby - 通过给定的正则表达式生成字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22251342/

相关文章:

ruby-on-rails - 电影院预订申请

javascript - 无法从字符串构建 RegExp 获得预期输出

Python正则表达式判断一个字符是数字、字母数字还是在指定的字符串中

javascript - 使用正则表达式计算字符串中句号和逗号的数量

php - 如何使用空格作为分隔符将字符串拆分为 5 个单词的部分

ruby - 在模块/类的末尾执行代码,如 Ruby 测试/单元

ruby - 建筑 gem - InvalidSpecificationException [...] 不是文件

ruby - 查找最接近数组平均值的元素

ruby-on-rails - 将文件上传到服务器并将路径存储在 Ruby on Rails 的数据库中

ruby: 优化 => phrase.split(delimiter).collect {|p| p.lstrip.rstrip }