百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

randexp

> 编程语言
开源

从正则表达式生成随机数据

286 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

从正则表达式生成随机数据

h1. randexp

bq. by Ben Burkert

h2. DESCRIPTION:

randexp makes it easy to generate random string from most regular expressions.

h2. REQUIREMENTS:

  • none!

h2. INSTALL:

$ sudo gem install randexp

h2. USAGE:

randexp adds the #generate (or #gen, for short) method to the Regexp class, which generates a 'random' string that will match your regular expression.

/abc|def/.gen # => "def"

h2. Valid Regexp's

randexp can only generate matching string from simple regular expression. Except for a few circumstances, wildcards are generally not allowed in the regular expression. is pretty domain specific, so trying to guess when to terminate a random pattern would produce unhelpful data:

/Aa{3}h*!/.gen # => RuntimeError: Sorry, "h*" is too vague, try setting a range: "h{0,3}" /Aa{3}h{3,15}!/.gen => "Aaaahhhhh!"

/(never gonna (give you up|let you down), )/.gen => RuntimeError: Sorry, "(...)" is too vague, try setting a range: "(...){0, 3}" /(never gonna (give you up|let you down), ){3,5}/.gen => "never gonna give you up, never gonna let you down, never gonna give you up, never gonna give you up, "

The exception being word characters (\w), which generate a random word from the Dictionary class.

/\w+/.gen => "groveling"

h3. Primitives & Complex matches

The single character matchers supported are words(\w), whitespace(\s), and digits(\d).

/\d{50}/.gen => "50315410741096763188525493528315906035878741451037"

When a multiplicity constraint is placed on a word character, a word with the valid length is generated.

/\w{10}/.gen # a word with 10 letters => "Chaucerism"

/\w{5,15}/.gen => "cabalistic"

Complex matchers use the [:...:] syntax within the regular expression.

/[:sentence:]/.gen => "Nonhearer demetricize toppiece filicic possessedness rhodizite zoomagnetism earwigginess steady"

Complex matchers can also be added by extending the Randgen class.

class Randgen def self.serial_number(options = {}) /XX\d{4}-\w-\d{5}/.gen end end

/[:serial_number:]/.gen => "XX3770-M-33114"

h3. Dictionary

The Dictionary loads the local users' words file, allowing randomly generated words to be chosen from thousands of entries to the words file. Words are mapped by their length to allow words to be randomly chosen based on size.

h3. Real Names

One can easily generate real names based on a wordlist.

Random first name (male or female):

Randgen.first_name => "Malika"

Random first name, with a specific gender:

Randgen.first_name(:gender => :male) => "Rick"

Random first name, with a specific gender & length:

Randgen.first_name(:gender => :male, :length => 10) => "Kristopher"

Random last name:

Randgen.last_name => "Klocko"

Random name:

Randgen.name => "Rosendo Raynor"

alternative:

/[:name:]/.gen => "Marc Adams"

Random name, with a specific gender:

Randgen.name(:gender => :female) => "Tatiana Brakus"

h4. Contributors

  • Matt Aimonetti (mattetti)
  • Gerrit Kaiser (gerrit)
  • Mike Vincent (shame)

Issues· 10 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言