Reputation: 2975
I am trying to write a regular expression in eclipse which recognizes the string
X.printStackTrace();
where 'X' will be the exception variable. My expression is as follows:
([^\*\/\/\s])(.*)\Q.printStackTrace();\E
This works correctly except on the 3rd line when there are multiple printStackTrace statements in single line.
/* e.printStackTrace();
e.getMessage();
e.printStackTrace();e.printStackTrace(); e.printStackTrace();
e.printStackTrace();*/
/* e.printStackTrace();
e.getMessage();*/
/*
// e.printStackTrace();
e.getMessage();
*/
Can anyone let me know what should be the regular expression if i have to search only
X.printStackTrace();
on the 3rd line?
Upvotes: 0
Views: 200
Reputation: 11233
You might be looking at this:
((?:\w+.printStackTrace\(\);\s*)+?)
Check out matches here.
EDIT : (my regex output at Rubular)
/* e.printStackTrace();
e.getMessage();
e.printStackTrace();e.printStackTrace(); e.printStackTrace();
e.printStackTrace();*/
/*e.printStackTrace();
e.getMessage();/ /
//e.printStackTrace();
e.getMessage(); */
EDIT
For that we need to adjust the pattern to make greedy (+)
to ungreedy (+?)
.
Upvotes: 0
Reputation: 48807
The problem is in the (.*)
part: you have to lazy match using (.*?)
.
I would have used the following regex though: [^.\s]+\Q.printStackTrace();\E
Upvotes: 1