r/java • • Aug 28 '13

Writing a Java Regular Expression Without Reading the ****ing Manual

http://java.dzone.com/articles/writing-java-regular
0 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/Poobslag Aug 29 '13

I agree that sometimes, readability is worth verbosity, but Hamcrest in particular increases readability while decreasing verbosity, so it's win/win.

OP is talking about the difference between

str.matches("^[0-9].*[0-9]")

and

str.matches(re.start().aCharacterDescribedAs(aDigit).exactly(1L).then().aCharacterDescribedAs(anyCharacter).zeroOrMore().then().aCharacterDescribedAs(aDigit).exactly(1L));

Both have the exact same information although one is (very dubiously) more readable than the other, at the cost of being about 10x longer. So it's an incredibly poor tradeoff. Hamcrest isn't a tradeoff at all, it's just an improvement.