import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "String\\.fromCharCode[^()]*\n"
+ "(\n"
+ " \\(\n"
+ " (?:[^()]|(?1))*\n"
+ " \\)\n"
+ ")";
final String string = "\n\n\n"
+ "I already saw this answer : How to get parentheses inside parentheses but it didn't really work if I don't know the number of levels of those parentheses.\n\n"
+ "I'm actually trying to deobfuscate a js file with python, and I have this kind of string that I want to \"scrap\" :\n\n"
+ "String.fromCharCode\n"
+ " (\n"
+ " (010 * 12 + 6),\n"
+ " (06 * (0x1 * (1 * 0xa + 6) + 1) + 12),\n"
+ " (4 * 27 + 3),\n"
+ " (01 * 0x3b + 50),\n"
+ " (1 * 0x34 + 15),\n"
+ " (1 * (1 * (3 * ((0x1 * 8 + 7) * 1 + 0) + 8) + 24) + 27),\n"
+ " (0x1 * (2 * 0x25 + 7) + 16),\n"
+ " (1 * 0112 + 40),\n"
+ " (1 * 0x2c + 23),\n"
+ " (0x3 * 042 + 9),\n"
+ " (1 * ((05 * 4 + 1) * 03 + 0) + 37),\n"
+ " (0x2 * (1 * 0x1f + 4) + 31)\n"
+ " )\n\n"
+ "When I run : re.findall(r\"String.fromCharCode\\((.+?)\\)\", content) it returns me String.fromCharCode((03 * (07 * 4 + 3) at first. So it seems like my line of code is only searching for the first occurrence of a closed parenthesis. I didn't try the answer of the above link but it seems like to not be \"infinite\", we should know beforehand the number of levels.\n\n"
+ "And what I want to get is the whole parenthesis like that : ((010 * 12 + 6),(06 * (0x1 * (1 * 0xa + 6) + 1) + 12),(4 * 27 + 3),(01 * 0x3b + 50),(1 * 0x34 + 15),(1 * (1 * (3 * ((0x1 * 8 + 7) * 1 + 0) + 8) + 24) + 27),(0x1 * (2 * 0x25 + 7) + 16),(1 * 0112 + 40),(1 * 0x2c + 23),(0x3 * 042 + 9),(1 * ((05 * 4 + 1) * 03 + 0) + 37),(0x2 * (1 * 0x1f + 4) + 31))\n\n"
+ "EDIT:\n\n"
+ "To clarify, the code have many other occurrence of the \"String.fromCharCode\" that is above. If I were to delete the ? in the regex code, it will retrieve the entire code.\n\n"
+ " ";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html