私の正規表現パターンは次のとおりです。正規表現:同じパターンで文字列置換を行うが、異なる交換言葉で
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."
私の実装では、文字列内のすべての一致する単語をと置き換えて同じを置き換えています。すなわち:
@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.
最初の文字列と2EDの間にどのような違い?あなたはそのパターンで何をしたいですか? –
私はまだ正規表現の初心者ですので、パターンが間違っているかもしれません。しかし基本的に私は辞書(Key-> Value)/(Target - > Replacements)を持っており、keyをvalueに置き換えたいと思っています。いくつかのサンプルデータ:{([TRSP3058 - > TRSG0013])、 (TRSP309E - > \t TRSG0014)、 (TRSP30BC - > TRSG0015)} –