You can compare Pattern
objects by comparing the result of calling pattern()
or toString
but this doesn't do what you want (if I understand your question correctly). Specifically, this compares the strings that were passed to the Pattern.compile(...)
factory method. However, this takes no account of flags passed separately to the pattern string.
There is no simple way to test if two non-identical regexes are equivalent. For example ".+"
and "..*"
represent equivalent regexes, but there is no straight-forward way to determine this using the Pattern
API.
I don't know if the problem is theoretically solvable ... in the general case. @Akim comments:
There is no finite axiomatization to regex equivalence, so the short answer is "this is not doable by tree transformations of the regexes themselves". However one can compare the languages of two automata (test their equality), so one can compute whether two regexes are equivalent. Note that I'm referring to the "genuine" regexes, with no extensions such as back-references to capture groups, which escape the realm of rational languages, i.e., that of automata.
I also want to comment on the accepted answer. The author provides some code that he claims shows that Pattern's equals
method is inherited from Object
. In fact, the output he is seeing is consistent with that ... but it doesn't show it.
The correct way to know if this is the case is to look at the javadoc ... where the equals
method is listed in the list of inherited methods. That is definitive.
So why doesn't the example show what the author says it shows?
It is possible for two methods to behave the same way, but be implemented differently. If we treat the Pattern
class as a black box, then we cannot show that this is not happening. (Or at least ... not without using reflection.)
The author has only run this on one platform. Other platforms could behave differently.
On the second point, my recollection is that in the earlier implementation of Pattern
(in Java 1.4) the Pattern.compile(...)
methods kept a cache of recently compiled pattern objects1. If you compiled a particular pattern string twice, the second time you might get the same object as was returned the first time. That would cause the test code to output:
true
true
true
true
But what does that show? Does it show that Pattern
overrides Object.equals
? No!
The lesson here is that you should figure out how a Java library method behaves primarily by looking at the javadocs:
If you write a "black box" test, you are liable to draw incorrect conclusions ... or at least, conclusions that may not be true for all platforms.
If you base your conclusions on "reading the code", you run the risk of drawing conclusions that are invalid for other platforms.
1 - Even if my recollection is incorrect, such an implementation would be consistent with the javadocs for the Pattern.compile(...)
methods. They do not say that each compile
call returns a new Pattern
object.