You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: Please check the original repo. It is kept up to date :)
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 URLstester=VerEx.newdostart_of_linefind'http'maybe's'find'://'maybe'www.'anything_but' 'end_of_lineend# Create an example URLtest_url="https://www.google.com"# Use it just like a regular Ruby regex:puts'Hooray! It works!'iftester.match(test_url)puts'This works too!'iftester =~ test_url# Print the generated regex:putstester.source# => /^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/
Replacing strings
# Create a test stringreplace_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 objectresult=replace_me.gsub(expression,"duck");putsresult# Outputs "Replace duck with a duck"
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. Unforunately, 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!