2

I use a tool where a large number of strings (stored in a database) are transformed by a series of Java regex rules to be more human-readable. To make it easier to add new rules, I want a UI that shows which strings will be affected by a proposed regex.

I have a school project where one of the few requirements is that it's done with Ruby on Rails and am looking at using it to fill my above need.

Is there an existing library/gem that will convert a proposed Java-style regex to a Ruby regex (so that the code that finds affected strings can be in ruby)?

Edit: I guess I didn't explain myself very well. I'm looking for a gem that converts any given Java regex to the equivalent Ruby regex (within limits of course). Here's an example of the workflow:

  1. User enters the Java regex, *[^~]Some.Regex[1-9]*
  2. The java regex string is sent to rails server
  3. The rails server returns a list of the strings from the database that this regex applies to.

In looking at ways to accomplish step three, I was wondering if there is a library that will convert the java regex *[^~]Some.Regex[1-9]* to the ruby equivalent, /*[^~]Some.Regex[1-9]*/ (not sure if this is actually the equivalent, but just using it for the sake of an example).

1
  • 1
    What is the regular expression you would like to convert? Examples are important. Keep in mind that writing something like Rubular is not necessarily a trivial task. Commented Mar 14, 2013 at 2:40

3 Answers 3

2

If I remember right, Java regex are basically strings, passed to a method, which passed them to the regex engine.

Ruby's regex really are no different. Ruby has several ways of creating a regular expression, all of which return the same object, though they are defined differently:

/[^~]Some.Regex[1-9]*/             # => /[^~]Some.Regex[1-9]*/
%r[[^~]Some.Regex[1-9]*]           # => /[^~]Some.Regex[1-9]*/
Regexp.new('[^~]Some.Regex[1-9]*') # => /[^~]Some.Regex[1-9]*/

pattern = '[^~]Some.Regex[1-9]*'
/#{ pattern }/                     # => /[^~]Some.Regex[1-9]*/

That gives you some flexibility and lets you substitute a string into the regex or pass it to Regex.new.

Ruby doesn't have all the flags that Java does, so you'll need to watch out for those. The non-capturing group and look-ahead/look-behind and assertions are supported:

(?:X)               X, as a non-capturing group
(?idmsux-idmsux)    Nothing, but turns match flags i d m s u x on - off
(?idmsux-idmsux:X)  X, as a non-capturing group with the given flags i d m s u x on - off
(?=X)               X, via zero-width positive lookahead
(?!X)               X, via zero-width negative lookahead
(?X)               X, as an independent, non-capturing group
Sign up to request clarification or add additional context in comments.

Comments

0

You should check out http://txt2re.com/

It will allow you to generate the REGEX based on the selections you choose. You can then select the language you wish to use the REGEX on. This will allow you to compare how Java writes the expression verses Ruby. It is a great tool and hope that it's owner keeps it up for a long time!

enter image description here

Comments

0

I ended up making a gem to support this. The github page has details on which features it supports.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.