0

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!

Community
  • 1
  • 1
Qwe Asd
  • 3
  • 1

1 Answers1

0

There are a few problems here:

  1. "^(.*)\\{([^\\{\\}]+)\\}(.*)$" will match the whole string if a match can be found, then matcher.group() called without group number will take the string that matches the whole regex (in this case, the whole string) and feed it to your select function.

    For this, you only need to match the {} and whatever inside it "\\{([^{}]+)\\}", and take the content inside {} (which is in capturing group 1) and give it to select

    Pattern reg = Pattern.compile("\\{([^{}]+)\\}");
    ...
    while (matcher.find()) {
        spun = matcher.replaceFirst(select(matcher.group(1)));
    
  2. Random.nextInt(int) "returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)" (emphasis mine).

    You should have written int index = random.nextInt(choices.length);. Otherwise, the last choice will never be chosen.

  3. And assigning spun to the replaced value doesn't change the string used by the Matcher. You you need to reset the Matcher to use the new string in spun, as explained in the documentation of Matcher.replaceFirst(String):

    Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.

    You reset the matcher's state and tell it to use the new string in spun by calling Matcher.reset(CharSequence):

    while (matcher.find()) {
        spun = matcher.replaceFirst(select(matcher.group(1)));
        matcher.reset(spun);
    }
    
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • nhahtdh, you're right! Thanks!!! But sometimes "This string to generate page" without "1." or "2!" or "3?".??? – Qwe Asd Oct 07 '14 at 08:00
  • @QweAsd: It is specified by your string. You probably want `"This {script|string} to generate {on {page|texts} {1.|2!|3?}}"` – nhahtdh Oct 07 '14 at 08:19
  • nhahtdh, all right! Thanks!!! Thanks!!! Thanks!!! I can not tell you to put a "this answer is useful" because they do not have 15 reputation. All the same, thank You! Everything works fine. – Qwe Asd Oct 07 '14 at 08:52