我的正则表达式如下:
socket.emit
对于像这样的测试字符串:
public final static String REGEX_PATTERN = "\\bTRS[S|P|M]....\\b";
我期待此字符串返回:
"Hey there! I think TRSS190E is a very important parameter for the rover. Because the Martian atmosphere also requires TRSP1143 and TRSM0146 for it's platform and mobility subsystems."
但是我的实现是用相同的替换替换字符串中的每个匹配单词。即:
"Hey there! I think TRST0822 is a very important parameter for the rover. Because the Martian atmosphere also requires TRSP6644 and TRSM1273 for it's platform and mobility subsystems."
测试代码如下:
Hey there! I think TRST0822 is a very important parameter for the rover. Because the Martian atmosphere also requires TRST0822 and TRST0822 for it's platform and mobility subsystems.
字符串实用程序方法:
@Test
public void performRegexReplacement() {
// Construct a test mapper/dictionary
List<aMap> aMaps = new ArrayList<aMap>();
aMaps.add(new aMap(new String[] {"TRSS190E", "TRST0822"}));
aMaps.add(new aMap(new String[] {"TRSP1143", "TRSP6644"}));
aMaps.add(new aMap(new String[] {"TRSM0146", "TRSM1273"}));
Mapper mapper = new Mapper(aMaps);
// Perform replacement
String corpus = "Hey there! I think TRSS190E is a very important parameter for the rover. " +
"Because the Martian atmosphere also requires TRSP1143 and TRSM0146 for it's " +
"platform and mobility subsystems.";
String expectedCorpus = "Hey there! I think TRST0822 is a very important parameter for the rover. " +
"Because the Martian atmosphere also requires TRSP6644 and TRSM1273 for it's " +
"platform and mobility subsystems.";
String[] find = new String[] {"TRSS190E", "TRSP1143", "TRSM0146"};
List<String> matchingTargets = StringUtils.getPatternMatchingWords(corpus);
System.out.println("matchingTargets: "+matchingTargets.toString());
List<String> replacements = new ArrayList<>();
for(String matchingTarget : matchingTargets) {
// search mapper for replacement str
replacements.add(mapper.linearSearch(matchingTarget));
}
System.out.println("replacements: "+replacements.toString());
String updatedCorpus = StringUtils.replaceWords(corpus, matchingTargets, replacements);
assertEquals(expectedCorpus, updatedCorpus);
}
控制台输出:
public static List<String> getPatternMatchingWords(String text) {
final Pattern pattern = Pattern.compile(REGEX_PATTERN);
final Matcher matcher = pattern.matcher(text);
List<String> matchedWords = new ArrayList<>();
while (matcher.find()) {
String fullMatch = matcher.group(0);
matchedWords.add(fullMatch);
}
return matchedWords;
}
public static String replaceWords(String text, List<String> targets, List<String> replacements) {
// StringBuilder sb = null;
System.out.println("targets: "+targets.toString());
int i = 0;
String str = null;
for(String target : targets) {
str = replaceWord(text, target, replacements.get(i));
i++;
}
System.out.println(str);
return str;
}
/**
* Replaces all instances of a matching word in text.
* @param text
* @param target
* @param replacement
* @return <code>String</code> containing replacement(s)
*/
public static String replaceWord(CharSequence text, String target, String replacement) {
final Pattern pattern = Pattern.compile(REGEX_PATTERN);
final Matcher matcher = pattern.matcher(text);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String txt = matcher.group(0);
matcher.appendReplacement(sb, replacement);
}
matcher.appendTail(sb);
System.out.println(sb.toString());
return sb.toString();
}
答案 0 :(得分:1)
如果我理解正确,你想要别人替换某些单词。
我建议您将所有单词及其替换放在同一个地图中并运行以下内容:
public static String replaceWords(String text, Map<String,String> replacement) {
String temp = text;
for(Entry<String,String> entry : replacement.entrySet()){
temp = temp.replace(entry.getKey(), entry.getValue());
}
return temp;
}
如果你想要替换litteral Strings,则不需要REGEX(至少,不是你看到的)。
编辑:
使用上面的函数,你的测试用例(但没有junit):
public static void performRegexReplacement() {
// Construct a test mapper/dictionary
List<Map<String, String>> dictionnary = Arrays.asList(Collections.singletonMap("TRSS190E", "TRST0822"), Collections.singletonMap("TRSP1143", "TRSP6644"), Collections.singletonMap("TRSM0146", "TRSM1273"));
// Perform replacement
String corpus = "Hey there! I think TRSS190E is a very important parameter for the rover. " +
"Because the Martian atmosphere also requires TRSP1143 and TRSM0146 for it's " +
"platform and mobility subsystems.";
String expectedCorpus = "Hey there! I think TRST0822 is a very important parameter for the rover. " +
"Because the Martian atmosphere also requires TRSP6644 and TRSM1273 for it's " +
"platform and mobility subsystems.";
String updatedCorpus = corpus;
for(Map<String,String> replacement : dictionnary){
updatedCorpus = replaceWords(updatedCorpus, replacement);
}
System.out.println(updatedCorpus);
if(expectedCorpus.equals(updatedCorpus)){
System.out.println("yay");
} else {
System.out.println("no");
}
}