I have the below string in the variable jss.
jss = '''
Java.perform(
function()
{
var item = Java.use("java.util.Random");
console.log("HOOKING random");
item.nextInt.overload("int").implementation = function(a)
{
var ret = this.nextInt(a);
return {0};
}
}
);
'''.format("1234")
and I am trying to use a format specifier to simply pass it some value ("1234" in this case).
When I try running this in iPython, I get the following error:
ERROR:root:An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line string', (1, 0))
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-1-a900f760c449> in <module>()
12 }
13 );
---> 14 '''.format("1234")
KeyError: '\n var item = Java'
Not sure what is going wrong here. Can someone please help understand?
you need to escape your other braces with {{
and }}
- otherwise the python format tokenizer wants to make sense of the pattern. (only tested in python3...)
jss = '''
Java.perform(
function()
{{
var item = Java.use("java.util.Random");
console.log("HOOKING random");
item.nextInt.overload("int").implementation = function(a)
{{
var ret = this.nextInt(a);
return {0};
}}
}}
);
'''.format("1234")
assuming the braces you do not want to use as formatting options are always preceeded by more than one whitespace (and the format braces are preceeded by one whitespace at most) you could replace those:
import re
# with your original jss
sanitized = re.sub('(\s\s+){', r'\1{{', jss)
sanitized = re.sub('(\s\s+)}', r'\1}}', sanitized)
print(sanitized.format("1234"))
...there may be much more sensible regular expressions to suit your needs...