我想解析一些 C 源文件并找到所有字符串(“foo”)。
类似的东西有效
String line = "myfunc(\"foo foo foo\", \"bar\");";
System.out.println(line);
String patternStr = "\\\"([^\"]+)\\\"";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher("");
String s;
if(line.matches(".*"+patternStr+".*"))
matcher.reset(line);
while(matcher.find()) {
System.out.println(" FOUND "+matcher.groupCount()+" groups");
System.out.println(matcher.group(1));
}
直到没有像这样的“转义引号字符串”
String line = "myfunc(\"foo \\\"foo\\\" foo\", \"bar\");";
我不知道如何在 Java 中创建表达式,例如“没有\”但有\。” 我在这里找到了与 C 类似的东西 http://wordaligned.org/articles/string-literals-and-regular-expressions
提前致谢。
最佳答案
注释中的字符串怎么样:
/* foo "this is not a string" bar */
当评论中出现单双引号时会怎样:
/* " */ printf("text");
您不想将 "*/printf("
捕获为字符串。
换句话说:如果上述可能出现在您的 C 代码中,请使用解析器而不是正则表达式。
关于java - 使用java在C代码中搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2237126/