我正在尝试用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.
任何帮助将不胜感激!
我建议你做一些更简单的事情,例如
// 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.
有些人在遇到问题时会想"我知道,我会使用正则表达式"。现在他们有两个问题。- 杰米·扎温斯基 - 1997
当然,你也可以做
public static String tagDigits(String in) {
if (in == null) {
return "";
}
return in.replaceAll("\d+", "<mo>$0</mo>");
}
原始
字符串中匹配部分的索引与前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
匹配。