CrazyCoder
CrazyCoder

Reputation: 2605

What is the Java equivalent of Regexp::Assemble?

I came across the Perl module Regexp::Assemble which takes an arbitrary number of regular expressions and assembles them into a single regular expression that matches all that the individual ones.

Is there any similar library for Java? It will be quite tedious to write a combined regex for each and every input.

Upvotes: 4

Views: 395

Answers (3)

Simon
Simon

Reputation: 1

For my use case, I've written a script with Regexp::Assemble which will generate the set of REs I want and write them to a file, to be read in by the main java app - would that work for you ?

Upvotes: 0

beerbajay
beerbajay

Reputation: 20270

I think this is just a matter of ORing the regexes. e.g. if you have the regexes [abc]+ and [xyz]+ you could combine these with (regex1|regex2) which is: ([abc]+|[xyz]+). Adding more regexes is just a matter of adding more | clauses. So you don't really need a 'generator' so much as a loop. For example:

import org.apache.commons.lang.StringUtils;

List<String> regexlist = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(file));  
String line = null;
while ((line = br.readLine()) != null) {  
    regexlist.add(line);
} 

String regex = "(?:" + StringUtils.join(regexlist, "|") + ")";

Upvotes: 0

Sap
Sap

Reputation: 5301

regexp might help you... The link points to an example that does similar to what you want, I am not sure if it will work with Regex dictionary or not though. Also, you might want to email the author and I am sure he can help you figure out a solution for your exact use case.

Upvotes: 2

Related Questions