There are tools that can do this ready, and they are called software conversion tools .
If you really want to recognize them as a serial line, with our DMS Software Reengineering Toolkit software conversion engine using your rule specification language (RSL). RSL allows you to write patterns in terms of the syntax of the language of the language whose grammar is already known to DMS:
domain Java~v7; pattern odd_pattern(IDENTIFIER: o, qualifier: something, IDENTIFIER: foo IDENTIFIER: bar, IDENTIFIER: foobar, more: stmt_sequence): stmt_sequence = " Object \o = \something.\foo; \bar(\o); \something.\foobar(); \more";
This defines the pattern in terms of the surface syntax of the indicated notation ("domain Java ~ v7"). Each template has a name ("odd_pattern") so you can distinguish between many templates. The template has a set of formatted (pure grammar) syntactic categories specified by sub-templates, referred to as <(non) terminal: name>. This template has several different identifiers, name patterns o, foo, bar, foobar. I guessed that you meant that something was a kind of qualification way, but perhaps you meant that it was also an identifier.
The contents of the template are defined in metacites " to distinguish the text of the RSL template language from the text of the target language (Java) template. Inside meta-pictures \ baz means the syntax template variable baz category N, where baz was defined as N: baz in the template parameter list. One and the same however, a template variable that occurs in several places requires identical substitutions.
Given this pattern, you can ask DMS where the (Java) AST corresponds. Alignment with AST, as well as white space problems, comments, unnormalized strings / identifiers / numbers / etc. eliminated. Just by counting matches, you get the original desired OP score. [We basically completed the extension of RSL templates in which elements are connected by data streams, rather than code syntax].
Presumably, he wants to convert these instances into some other code that he can do by writing in RSL,
rule rewrite_odd_pattern(parms): stmt_sequence odd_pattern(parms) -> replacement_pattern(parms);
mention of some other "replacement_pattern" written in a similar way.
If the OP has only a small number of such patterns, it probably makes better use of the regular expression or just punches it rather than getting the DMS (or other program conversion tool) and tweaking it. If he does this a lot, or they are scattered across very large heaps of code, the templates are complex, or he has many similar things, then a compromise may overturn another way.