Code Monkey home page Code Monkey logo

randexp's Introduction

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)

randexp's People

Contributors

benburkert avatar atmos avatar sr avatar mjackson avatar gerrit avatar mattetti avatar snusnu avatar jakuboboza avatar shame avatar

Watchers

James Cloos avatar Alves Lobo Michael avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.