如何用Math M1标签包围数字?

时间:2013-12-30 01:37:13

标签: java xml regex

我试图用XML标签包围数字。我希望我的输出为<mo>11</mo>。相反,我得到了最奇怪的输出。 下面是我的代码,然后是奇怪的输出。

package javaapplication8;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Program {

    static String phrase = "Hi, I am number 123 just so you know. And, he is 11, also,     just so you know.";
    static StringBuilder builder = new StringBuilder(phrase);
    static final Pattern myPattern = Pattern.compile("\\d+");
    static final Matcher myMatcher = myPattern.matcher(phrase);

    public static void main(String[] args) {

        while (myMatcher.find()) {
            builder.replace(myMatcher.start(), myMatcher.end(), "<mo>" + myMatcher.group() + "</mo>");
        }
        System.out.println(builder.toString());
    }
}

输出:

Hi, I am number <mo>123</mo> just so you know. An<mo>11</mo> he is 11, also, just so you know.  

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

我建议你做一些更简单的事情,例如

// Add a <mo> </mo> tag around numerical runs in input.
public static String tagDigits(String in) {
  StringBuilder sb = new StringBuilder();
  boolean inDigitRun = false;
  for (char ch : in.toCharArray()) {
    if (!inDigitRun) {
      if (Character.isDigit(ch)) {
        sb.append("<mo>");
        inDigitRun = true;
      }
    } else {
      if (!Character.isDigit(ch)) {
        inDigitRun = false;
        sb.append("</mo>");
      }
    }
    sb.append(ch);
  }
  return sb.toString();
}

public static void main(String[] args) {
  String phrase = "Hi, I am number 123 just so you know. "
      + "And, he is 11, also,     just so you know.";
  System.out.println(tagDigits(phrase));
}

将输出

Hi, I am number <mo>123</mo> just so you know. And, he is <mo>11</mo>, also,     just so you know.
有些人在遇到问题时会想“我知道,我会使用正则表达式。”现在他们有两个问题...... - Jamie Zawinski - 1997

当然,您也可以这样做

public static String tagDigits(String in) {
  if (in == null) {
    return "";
  }
  return in.replaceAll("\\d+", "<mo>$0</mo>");
}

答案 1 :(得分:1)

原始字符串中匹配部分的索引与第一次replace操作后的字符串中的索引不同(您将"<mo>""</mo>"添加到此字符串中,以便将其后的字符移动到更远的位置)。尝试使用replaceAll方法

的不同方法
System.out.println(phrase.replaceAll("\\d+", "<mo>$0</mo>"));

输出:

Hi, I am number <mo>123</mo> just so you know. And, he is <mo>11</mo>, also,     just so you know.

我们在这里使用正则表达式\\d+来匹配数字并将其放在组0中。稍后在替换部分中,我们可以通过$0运算符引用组0中的匹配。