Using a regular expression to capture arbitrary numbers would be (see online demo here):
String input= "$.store.book[Random(1,9)].title";
System.out.println("Input: "+ input);
Pattern p = Pattern.compile("(?<=\\[)Random\\((\\d+),(\\d+)\\)(?=\\])");
Matcher m = p.matcher(input);
String output = input;
if(m.find()) {
int min = Integer.valueOf(m.group(1));
int max = Integer.valueOf(m.group(2));
int rand = min + (int)(Math.random() * ((max - min) + 1));
output = output.substring(0, m.start()) + rand + output.substring(m.end());
}
System.out.println("Output: "+ output );
Example output:
Input: $.store.book[Random(1,9)].title
Output: $.store.book[6].title
As a utility method:
Online demo for code below.
public static String replaceRandom(String input) {
Pattern p = Pattern.compile("(?<=\\[)Random\\((\\d+),(\\d+)\\)(?=\\])");
Matcher m = p.matcher(input);
String output = input;
if (m.find()) {
int min = Integer.valueOf(m.group(1));
int max = Integer.valueOf(m.group(2));
int rand = min + (int)(Math.random() * ((max - min) + 1));
output = output.substring(0, m.start()) +rand+ output.substring(m.end());
}
return output;
}
public static void main(String[] args) {
System.out.println("(1,9): "
+ replaceRandom("$.store.book[Random(1,9)].title"));
System.out.println("(1,999): "
+ replaceRandom("$.store.book[Random(1,999)].title"));
System.out.println("(50,200): "
+ replaceRandom("$.store.book[Random(50,200)].title"));
}
Example output:
(1,9): $.store.book[4].title
(1,999): $.store.book[247].title
(50,200): $.store.book[71].title