I am using regex to go through some VB code and I want to find statements of the form –
ABC.Transaction = GlobalCommArea
Unfortunately, some statements are commented out in the code. VB single line comments begin with a single quote, so my search gives results like –
' ABCD.Transaction = GlobalCommArea <-- incorrect
' PQR.Transaction = GlobalCommArea <-- incorrect
WXY.Transaction = GlobalCommArea <-- correct
WXY.Transaction = GlobalCommArea ' 2012 <-- correct
I tried to detect for the presence of a single quote and exclude it, using the following code –
public static void test2()
{
String[] lines = new String[20];
lines[0] = "' ABCD.Transaction = GlobalCommArea";
lines[1] = " ' PQR.Transaction = GlobalCommArea";
lines[2] = " WXY.Transaction = GlobalCommArea";
lines[3] = "WXY.Transaction = GlobalCommArea ' 2012";
String regex;
regex = "^\\s*[^']*\\s*.*.Transaction\\s*=\\s*GlobalCommArea"; // the regex that I am using
Pattern p = Pattern.compile(regex);
for(int i=0; i<=3; i++)
{
Matcher m = p.matcher(lines[i]);
if(m.find())
{
System.out.print("Yes\t");
}
else
{
System.out.print("No\t");
}
System.out.println(lines[i]);
}
}
However, the regex didn’t work. I got the following output –
Yes ' ABCD.Transaction = GlobalCommArea
Yes ' PQR.Transaction = GlobalCommArea
Yes WXY.Transaction = GlobalCommArea
Yes WXY.Transaction = GlobalCommArea ' 2012
How to write a regex that will detect the single quote at the beginning of the line (i.e. excluding whitespace) and avoid those lines?
You are matching all of them because of the
.*beforeTransaction, try changing it to the following: