dot = "."
num = "25" D5 | `2 D4 D | maybe D1 1,2*D
match BOS num dot num dot num dot num EOS end
In your Ruby code, you can create a Regexador âscriptâ or âprogramâ
(probably by means of a here-document) that you can then pass into
the Regexador class. At minimum, you can convert this into a ârealâ
Ruby regular expression; there are a few other features and functions,
and more may be added.
So here is a complete Ruby program:
require 'regexador'
program = <<-EOS
dot = "."
num = "25" D5 | `2 D4 D | maybe D1 0,2*D
match WB num dot num dot num dot num WB end
EOS
pattern = Regexador.new(program)
puts "Give me an IP address"
str = gets.chomp
rx = pattern.to_regex # Can retrieve the actual regex
if pattern.match?(str) # ...or use in other direct ways
puts "Valid"
else
puts "Invalid"
end
This looks like a fun project which Ill look into.
I think youve made regexes look worse than they need to (though that
might well be how a person unfamiliar with regexes actually uses them).
The comments below are about regexes rather than your project.
I think it is possible to achieve a lot with interpolation in Rubys
regular expressions, remembering that \A and \z are the real end of
string anchors, and using the x modifier.
I had seen Verbal Expressions before I started my own project, but
never saw hexpress until a couple of weeks ago.
I think theyâre both worthy projects, as the concept itself is a worthy
one (in my opinion).
All three projects are similar in spirit and intent, but in
implementation
they are different. Obviously I like my own better. Arguably it is âmore
differentâ from these other two than they are from each other.