I looked How to create article spinner regex in Java?
From the string "This {script|string} to generate {on page|texts {1.|2!|3?}}"
need to get a randomize:
This script to generate on page 1.
or
This script to generate on page 2!
or
This script to generate on page 3?
or
This string to generate on page 1.
or ...
Here is the PHP code that supposedly worked:
function textGenerator($text)
{
static $result;
if (preg_match("/^(.*)\{([^\{\}]+)\}(.*)$/isU", $text, $matches))
{
$p = explode('|', $matches[2]);
foreach ($p as $comb)
textGenerator($matches[1].$comb.$matches[3]);
}
else
{
$result[] = $text;
return 0;
}
return array_values(array_unique($result));
}
$string = "This {script|string} to generate {on page|texts {1.|2!|3?}}"
I do so in Java:
String str = new String("This {script|string} to generate {page|texts {1.|2!|3?}}");
mTextView.setText(generateSpun(str));
public String generateSpun(String text){
String spun = text;
Pattern reg = Pattern.compile("^(.*)\\{([^\\{\\}]+)\\}(.*)$");
Matcher matcher = reg.matcher(spun);
while (matcher.find()){
spun = matcher.replaceFirst(select(matcher.group()));
}
return spun;
}
private String select(String m){
String[] choices = m.split("\\|");
Random random = new Random();
int index = random.nextInt(choices.length - 1);
return choices[index];
}
Returns the following: "This {script" or "2!" or "texts{1."
How can I get the right?
Thanks!