vendredi 3 avril 2015

How do you perform an intersection of two named regex character classes in Java?


Vote count:

0




I know how to find the intersection of two constant sets from this link to the Java tutorials.


Example:



[0-9&&[345]]


Will produce a character class containing all of the characters which intersect the set of single digit integers ('[0-9]') and a subset of that set containing the integers 3, 4, and 5 ('[345]').


This works fine for most needs, but it requires that both sets be known at compile-time. Let's say I'm writing a lexical analyzer for mathematical expressions and want to find the intersection between the set of unary operators and binary operators. Currently, the only operator that exists in both sets is '-'. As a binary operator '-' represents subtraction but as a unary operator it also represents negation. I anticipate that other operators may be pressed into dual-usage in the future, and want to develop a solution that not only works for the present case, but also the future case without changing the code. To do that, I need to be able to determine at run-time which operators exist in both sets.


Let's say I have two character classes defined as follows:



public static final String UNARYOPS = "[!~-]";
public static final String BINARYOPS = "[|&%^*/+-]";


How would I find the intersection between these two named character sets using the regex engine in Java so that I can identify which operators need to be checked for ambiguity between binary and unary?



asked 5 mins ago







How do you perform an intersection of two named regex character classes in Java?

Aucun commentaire:

Enregistrer un commentaire