There are tools that can do this off-the-shelf, and they are called program transformation tools.
If you really want to recognize these as consecutive line, with our DMS Software Reengineering Toolkit program transformation engine, using its Rule Specification Language (RSL). RSL lets you write patterns in terms of the language syntax of a 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 a pattern in terms of the surface syntax of the designated notation ("domain Java~v7"). Each pattern has a name ("odd_pattern") so that you can distinguish between many patterns. A pattern has a set of forced (clean grammar) syntax-category specified sub-patterns, named as <(non)terminal:name>. This pattern has several distinct identifiers, pattern-names o, foo, bar, foobar. I guessed you meant that something was some kind of qualifying path, but maybe you meant it to be just an identifier, too.
The pattern contents are defined in meta quotes " to distinguish the RSL pattern language text from the target language (Java) pattern text. Inside the meta quotes, \baz means the pattern variable baz of syntax category N where baz was defined as N:baz in the pattern parameter list. The same pattern variable occurring in multiple places requires identical substitutions.
Given this pattern, you can ask DMS where is matches a (Java) AST. By matching against an AST, al the problems of whitespace, comments, unnormalized strings/identifiers/numbers/etc. are eliminated. By simply counting matches, you get OP's original desired metric.
[We have largely completed an extension to RSL patterns in which the elements are connected by dataflows rather than code syntax].
Presumably he wants to transform these instances into some other code, which he can do by writing, in RSL,
rule rewrite_odd_pattern(parms): stmt_sequence
odd_pattern(parms) -> replacement_pattern(parms);
mentioning some other "replacement_pattern" written similarly.
If OP only has a modest number of such patterns, he's probably better using regex or just slugging it out rather than getting DMS (or another program transformation tool) and setting it up. If he does this a lot, or they are scattered over very big piles of code, the patterns are complex, or he has many similar kinds of things to do, the tradeoff might tip the other way.