简体   繁体   中英

Unable to create the Article Spinner regular expression in Java on Android?

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!

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); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM