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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度