github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

benburkert / randexp

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 97
    • 12
  • Source
  • Commits
  • Network (12)
  • Issues (2)
  • Downloads (3)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (3)
    • 0.1.3
    • 0.1.1
    • 0.1.0
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

generate random data from a Regular Expression — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

1.9 compatible 
atmos (author)
Tue Dec 01 11:34:46 -0800 2009
commit  22aad20822cac7a77f80a50b52edf761260455ac
tree    88eba0bdf51ef6ec6a223e5d1e5b16468231db4a
parent  0ce251893f1f10199e7b2927b2bcb7cc0eb9cd78
randexp /
name age
history
message
file .gitignore Tue Dec 01 11:14:53 -0800 2009 bundle the library for easier collaboration [atmos]
file CHANGELOG Wed Oct 08 20:41:58 -0700 2008 bumped the version number [benburkert]
file Gemfile Tue Dec 01 11:34:46 -0800 2009 1.9 compatible [atmos]
file LICENSE Tue Dec 01 11:14:53 -0800 2009 bundle the library for easier collaboration [atmos]
file README Mon Sep 08 09:33:55 -0700 2008 bumped the version number, updated the TODO & R... [benburkert]
file README.textile Wed Oct 08 18:19:31 -0700 2008 house cleaning + tweaked the README [benburkert]
file Rakefile Tue Dec 01 11:14:53 -0800 2009 bundle the library for easier collaboration [atmos]
file TODO Mon Sep 08 09:33:55 -0700 2008 bumped the version number, updated the TODO & R... [benburkert]
directory lib/ Tue Dec 01 11:34:46 -0800 2009 1.9 compatible [atmos]
directory spec/ Tue Dec 01 11:14:53 -0800 2009 bundle the library for easier collaboration [atmos]
directory wordlists/ Mon Sep 08 19:01:50 -0700 2008 refactored the RealName stuff a bit, thanks mat... [benburkert]
README.textile

randexp

by Ben Burkert

DESCRIPTION:

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

REQUIREMENTS:

  • none!

INSTALL:

$ sudo gem install randexp

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

  1. => “def”

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"

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"

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.

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"

Contributors

  • Matt Aimonetti (mattetti)
  • Gerrit Kaiser (gerrit)
  • Mike Vincent (shame)
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server