GithubHelp home page GithubHelp logo

musajoemo / verbal_expressions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryan-endacott/verbal_expressions

0.0 2.0 0.0 109 KB

Make difficult regular expressions easy! Ruby port of the awesome VerbalExpressions repo - https://github.com/jehna/VerbalExpressions

License: MIT License

verbal_expressions's Introduction

verbal_expressions

Build Status Gem Version

Ruby Regular Expressions made easy

VerbalExpressions is a Ruby library that helps to construct difficult regular expressions - ported from the awesome JavaScript VerbalExpressions.

How to get started

Just install with gem install verbal_expressions, then require the library and you're good to go!

require 'verbal_expressions'

Examples

Here's a couple of simple examples to give an idea of how VerbalExpressions works:

Testing if we have a valid URL

# Create an example of how to test for correctly formed URLs
tester = VerEx.new do
  start_of_line
  find 'http'
  maybe 's'
  find '://'
  maybe 'www.'
  anything_but ' '
  end_of_line
end

# Create an example URL
test_url = "https://www.google.com"

# Use it just like a regular Ruby regex:
puts 'Hooray!  It works!' if tester.match(test_url)
puts 'This works too!' if tester =~ test_url

# Print the generated regex:
puts tester.source # => /^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/

Replacing strings

# Create a test string
replace_me = "Replace bird with a duck"

# Create an expression that seeks for word "bird"
expression = VerEx.new { find 'bird' }

# Execute the expression like a normal Regexp object
result = replace_me.gsub( expression, "duck" );

puts result # Outputs "Replace duck with a duck"

Regex Capturing

# Grab the number of goals

tester = VerEx.new do
  find 'scored '
  begin_capture 'goals' # Can be named or unnamed
  word
  end_capture
end

match = tester.match('Jerry scored 5 goals!')
puts match['goals'] # => 5

# Alternative capture syntax

tester = VerEx.new do
  find 'scored '
  capture('goals') { word }
end

API documentation

I haven't added much documentation to this repo yet, but you can find the documentation for the original JavaScript repo on their wiki. Most of the methods have been ported as of v0.1.0 of the JavaScript repo. Just be sure to use the syntax explained above rather than the dot notation :)

Contributions

Clone the repo and fork! Pull requests are warmly welcomed!

Issues

  • I haven't yet ported the modifier code because Ruby Regexp handles modifiers a little differently.
  • Because or is reserved in Ruby, or is currently aliased to alternatively. Unfortunately, then is also reserved, so you must use find instead. I'm very open to better name ideas :)

Thanks!

Thank you to @jehna for coming up with the awesome original idea!

verbal_expressions's People

Contributors

ryan-endacott avatar vrish88 avatar

Watchers

James Cloos avatar John Moore 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.