gpt4 book ai didi

java - 正则表达式:使用相同模式但不同替换词执行字符串替换

转载 作者:行者123 更新时间:2023-12-01 08:52:27 25 4
gpt4 key购买 nike

我的正则表达式模式如下:

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();
}

控制台输出:

matchingTargets: [TRSS190E, TRSP1143, TRSM0146]
replacements: [TRST0822, TRSP6644, TRSM1273]
targets: [TRSS190E, TRSP1143, TRSM0146]
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.
Hey there! I think TRSP6644 is a very important parameter for the rover. Because the Martian atmosphere also requires TRSP6644 and TRSP6644 for it's platform and mobility subsystems.
Hey there! I think TRSM1273 is a very important parameter for the rover. Because the Martian atmosphere also requires TRSM1273 and TRSM1273 for it's platform and mobility subsystems.
Hey there! I think TRSM1273 is a very important parameter for the rover. Because the Martian atmosphere also requires TRSM1273 and TRSM1273 for it's platform and mobility subsystems.

最佳答案

如果我理解正确的话,你想用其他词替换某些词。

我建议您将所有单词及其替换放在同一个 map 中并运行以下命令:

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;
}

如果您想替换垃圾字符串,则不需要 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");
}
}

关于java - 正则表达式:使用相同模式但不同替换词执行字符串替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42300138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com