我正在使用正则表达式来处理一些 VB 代码,我想找到以下形式的语句 -
ABC.Transaction = GlobalCommArea
不幸的是,一些语句在代码中被注释掉了。VB 单行注释以单引号开头,所以我的搜索结果如下 -
' ABCD.Transaction = GlobalCommArea <-- incorrect
' PQR.Transaction = GlobalCommArea <-- incorrect
WXY.Transaction = GlobalCommArea <-- correct
WXY.Transaction = GlobalCommArea ' 2012 <-- correct
我尝试使用以下代码检测是否存在单引号并将其排除 -
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]);
}
}
但是,正则表达式不起作用。我得到以下输出 -
Yes ' ABCD.Transaction = GlobalCommArea
Yes ' PQR.Transaction = GlobalCommArea
Yes WXY.Transaction = GlobalCommArea
Yes WXY.Transaction = GlobalCommArea ' 2012
如何编写一个正则表达式来检测行首的单引号(即不包括空格)并避免这些行?