drsdvwsvo78320812 2014-10-07 05:58
浏览 30
已采纳

无法在Android上的Java中创建Article Spinner正则表达式?

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!

  • 写回答

1条回答 默认 最新

  • dqg63264 2014-10-07 07:34
    关注

    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);
      }
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。