2017-02-17 4 views
0

私の正規表現パターンは次のとおりです。正規表現:同じパターンで文字列置換を行うが、異なる交換言葉で

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. 
+0

最初の文字列と2EDの間にどのような違い?あなたはそのパターンで何をしたいですか? –

+0

私はまだ正規表現の初心者ですので、パターンが間違っているかもしれません。しかし基本的に私は辞書(Key-> Value)/(Target - > Replacements)を持っており、keyをvalueに置き換えたいと思っています。いくつかのサンプルデータ:{([TRSP3058 - > TRSG0013])、 (TRSP309E - > \t TRSG0014)、 (TRSP30BC - > TRSG0015)} –

答えて

1

私が正しく理解している場合、あなたが希望を次のように

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. 

テストコードがあります特定の単語をoで置き換えるthers。

私は、次の同じマップ内の単語のすべてと彼らの交換を入れて実行するようにお勧めしたい:あなたがしたい場合は(少なくとも、あなたが見ていないこと)

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の必要はありません。 litteral Stringsを置き換える。 、あなたのテストケースを(ただし、JUnitのなし)上記の関数を使用して

EDIT

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"); 
    } 
} 
+0

'Map'はオプションではありません。 TRSS4790] - > [TRSS4451])、 ([TRSS4451] - > [TRST2792])} 'Map'はK/Vの複製について不平を言って例外をスローします。 –

+0

ああ。次に、マップのリストを反復処理します。 –

+0

テストメソッド 'performRegexReplacement'の上位5行を見れば、基本的にはそれに相当します。 –

関連する問題