Brewing Patterns in Java: An Informal Primer

Not so long ago, Java lovers were engulfed by a mammoth addition — yes, we all know, right? — the Lambda Expressions in Java 1.8! Eclipsed by lambda, minor in its avatar for the oblivious, was another message — “should not be used as an identifier, since it is a reserved keyword from source level 1.8 on” — caught by the eyes of the assiduous and the pedantic; Wait, did I miss anything? hmm… Yes, like some of us who missed out that this article began with _, an underscore. An underscore was being removed as a legal identifier, quietly — and it was being promoted (ssshhh.. underscore does not know yet) to a more complex role — it was just another sign of times to come — the time has come to Match Patterns in Java!

ABCs of Pattern Matching

So what is pattern matching? All those Unix fans out there would suddenly remember the good old awk; Wikipedia says “AWK .. used as a data extraction...tool;… A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed.” — A Given Pattern is matched, Data is extracted and Some action is taken — with or without the data. If this sounds too involved, we will be surprised to know that we have been using this all along — The Find and Replace options all of us are very familiar with.