I do not know a tool that gives you a regular expression out of the box.
But since you have problems with CFG, I would recommend that you write a static analysis adapted to your problem. You can use a static analysis / bytecode mechanism like OPAL (Scala) or Soot (Java). You will find tutorials on every page of the project.
After setting up, you can load the target jar. You should be able to use the program control flow, as shown in the following example:
1 public static void example(String unknown) { 2 String source = "hello"; 3 if(Math.random() * 20 > 5){ 4 source += "world"; 5 } else { 6 source += "unknown"; 7 } 8 source += unknown; }
If your analysis detects a String or StringBuilder that is initialized, you can start building your regex. For example, line number two would cause your regular expression to say hello. If you encounter a conditional expression in the control flow of your program, you can parse each path and combine them through "|" later.
Then the branch: "peace" (line 4)
Another branch: "unknown" (line 6)
This can be reduced in line 7 to (the world) | (unknown) and add to the regular expression before the conditional.
If you encounter a variable, you can track it if you are doing interprocedural analysis or you have to use the wildcard operator “. *” Otherwise.
Final regex: "hello ((world) | (unknown)). *"
I hope this leads you to your decision that you want to achieve.
source share