Reputation: 4303
I am writing a migration utility, and a part of one of the conversions is testing a pair of regular expressions. For example, some of the tests will be:
+-------+-------+-------+
| Left | Right | Match |
+-------+-------+-------+
| (.)01 | 101 | Yes |
+-------+-------+-------+
| (.)02 | 101 | No |
+-------+-------+-------+
| 101 | 101 | Yes |
+-------+-------+-------+
| 201 | (.)01 | Yes |
+-------+-------+-------+
| (.)01 | 2(.)1 | Yes |
+-------+-------+-------+
At the moment, my test tests each by one using .matches
, which works when there is only one side is a regular expression, but when both sides have one (i.e. the last row of the example), it returns false when it should return true.
So, how can I get a positive comparison result for the last example?
Upvotes: 0
Views: 812
Reputation: 4737
You're using regular expressions wrong. You should use regexes to match Strings with a given regex, not to test regexes against each other.
A regex represents a set of possible matches: (.)01
matches a01
, 301
, $01
, etc, etc...
So, doing this makes sense when you match one item from that set, eg.$01
back against the regex.
In your last case, you're attempting to match a regex with a regex, which is just silly. Which regex is your source and which String is your target? If the first regex is your source, 201
matches it, but also 101
, #01
, etc... But this is not right according to the second regex, which matches items like 201
, but also 2#1
and 291
. So they should not be considered 'matching each other'.
Take a look at this Venn Diagram:
Your last regex match-up has two regexes fighting each other. The first regex is represented by circle A. The second regex is represented by circle B.
There are elements (well, just 201
) which are/is both in circle A and circle B (pointed out by the darker colored both A & B
). Would you consider these circles to be matching? I certainly don't. I would if they covered each other exactly.
But the only way for these circles to cover each other exactly (meaning everything in circle A is in circle B and everything in circle B is in circle A), is if both regexes are completely the same! Like (.)01
and...... (.)01
! This is the only possible match, but if you're treating one like a regex and one like a String, it still won't work.
EDIT If you just want to find whether this is at least one common match, this can be helpful: https://stackoverflow.com/a/17957180/1283166
Upvotes: 2
Reputation: 9946
You do not need any regular expression comparison in this scenario. A simple algorithm will work.
Remove characters based on wild card from both the strings and then use equals()
method to check.
Something like this might help you if there is no more than one occurrence of wild card in each string:
final String WILD_CARD = "(.)";
String str1 ="(.)01";
String str2 ="2(.)1";
int index = -1;
if((index=str1.indexOf(WILD_CARD))!=-1) {
str1 = str1.replace(WILD_CARD, "");
str2 = str2.replace(String.valueOf(str2.charAt(index)),"");
}
if((index=str2.indexOf(WILD_CARD))!=-1) {
str2 = str2.replace(WILD_CARD, "");
str1 = str1.replace(String.valueOf(str1.charAt(index)), "");
}
if(str1.equals(str2)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
Upvotes: 0