pacoverflow
pacoverflow

Reputation: 3881

Eclipse find won't do a non-greedy match

I have the following line of code in a Java file in Eclipse:

private Map<Integer, ComponentManager> compManagerMap;

When I hit CTRL+F and try to find omp.*?Map (with the Regular expressions box checked), Eclipse highlights omponentManager> compManagerMap instead of ompManagerMap.

I read that using a ? in the regular expression will result in a non-greedy match, but Eclipse is still doing a greedy match.

Upvotes: 0

Views: 1091

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174796

It does its job correctly. See the demo here. Note that matching starts from left and progresses towards the right.

So matching starts with the first omp upto next Map string, it does its job perfectly. If there were two Map strings, then it would return two matches. See the demo of non-greedy match here

If you want the second omp string to be matched then try the below regex,

omp(?:(?!omp).)*?Map

DEMO

It would match the substring ompManagerMap because it doesn't contain the string omp.

Pattern Explanation:

  • omp Matches the literal omp string.
  • (?:(?!omp).)*? Matches any charcter not of the string omp zero or more times. ? after * does a shortest match.
  • Map Matches the literal Map string.

Upvotes: 3

Related Questions