How to create article spinner regex in Java?

我的梦境 提交于 2019-12-04 19:35:48

Here are some of the problems with your current code:

  • You should reuse your compiled Pattern, instead of Pattern.compile every time
  • You should reuse your Random, instead of new Random every time
  • Be aware that String.split is regex-based, so you must split("\\|")
  • Be aware that curly braces in Java regex must be escaped to match literally, so Pattern.compile("\\{([^{}]*)\\}");
  • You should query group(1), not group() which defaults to group 0
  • You're using replaceFirst wrong, look up Matcher.appendReplacement/Tail instead
  • Random.nextInt(int n) has exclusive upper bound (like many such methods in Java)
  • The algorithm itself actually does not handle arbitrarily nested braces properly

Note that escaping is done by preceding with \, and as a Java string literal it needs to be doubled (i.e. "\\" contains a single character, the backslash).

Attachment

To fix the regex, add backslashes before the outer { and }. These are meta-characters in Java regexes. However, I don't think that will result in a working program. You are modifying the variable spun after it has been bound to the regex, and I do not think the returned Matcher will reflect the updated value.

I also don't think the python code will work for nested choices. Have you actually tried the python code? You say it "supposedly works", but it would be wise to verify that before you spend a lot of time porting it to Java.

Well , I just created one in PHP & Python , demo here http://spin.developerscrib.com , its at a very early stage so might not work to expectation , the source code is on github : https://github.com/razzbee/razzy-spinner

Use this, will work... I did, and working great

Pattern p = Pattern.compile("cat");
 Matcher m = p.matcher("one cat two cats in the yard");
 StringBuffer sb = new StringBuffer();
 while (m.find()) {
     m.appendReplacement(sb, "dog");
 }
 m.appendTail(sb);
 System.out.println(sb.toString());

and here

private String select(String m){
    String[] choices = m.split("|");
    Random random = new Random();
    int index = random.nextInt(choices.length - 1);
    return choices[index];
}

m.split("|") use m.split("\\|")

Other wise it splits each an every character

and use Pattern.compile("\\{([^{}]*)\\}");

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!