2016-05-13 18 views
3

この関数は、文字列内の特定の部分文字列をそれぞれの値に置き換えるために使用します。文字列内の複数のサブ文字列を置き換えます。

//マップ(string_to_replaceは、string_to_replace_with)

String template = "ola ala kala pala sala"; 
StringBuilder populatedTemplate = new StringBuilder(); 
HashMap<String, String> map = new HashMap<>(); 
map.put("ola", "patola"); 
map.put("pala", "papala"); 

int i=0; 
for (String word : template.split("'")) { 
    populatedTemplate.append(map.getOrDefault(word, word)); 
    populatedTemplate.append(" "); 
} 

System.out.println(populatedTemplate.toString()); 

ストリングを交換する場合は、この上記の機能は正常に動作しますが、「「(スペース)に囲まれています。

Ex-String => "Hey {how}は$ =あなた" 置き換えられる部分文字列が "Hey"または "you"の場合、正常に動作します。問題は、「どのように」と「あなた」を置き換えたいときです。

これをさらに複雑にすることなくどのように達成できますか?

+0

をなぜ 'template.replace(string_to_replaceは、string_to_replace_with)'として、多くの場合、あなたが必要として? PS:そうでなければ動作するかもしれないhttp://stackoverflow.com/questions/1324676/what-is-a-word-boundary-in-regexes – zapl

+0

@zaplその問題は、置換が依存していることです。つまり、「どのように」「存在する」、「存在する」を「大丈夫」に置き換えることができます。最初の反復文字列の後に "Hey {are} $ = you"となります。そして、2回目の繰り返しの後に "おい、お元気ですか$ =あなた"。 Andsは間違った出力。 – tarun14110

答えて

2

私はあなたがマップを持っているとそのまま残りを保つだけの単語を置き換えたい、あなたに次の続行することができます

String template = "Hey {how} are $=you"; 
StringBuilder populatedTemplate = new StringBuilder(); 
Map<String, String> map = new HashMap<>(); 
map.put("how", "HH"); 
map.put("you", "YY"); 
// Pattern allowing to extract only the words 
Pattern pattern = Pattern.compile("\\w+"); 
Matcher matcher = pattern.matcher(template); 
int fromIndex = 0; 
while (matcher.find(fromIndex)) { 
    // The start index of the current word 
    int startIdx = matcher.start(); 
    if (fromIndex < startIdx) { 
     // Add what we have between two words 
     populatedTemplate.append(template, fromIndex, startIdx); 
    } 
    // The current word 
    String word = matcher.group(); 
    // Replace the word by itself or what we have in the map 
    populatedTemplate.append(map.getOrDefault(word, word)); 
    // Start the next find from the end index of the current word 
    fromIndex = matcher.end(); 
} 
if (fromIndex < template.length()) { 
    // Add the remaining sub String 
    populatedTemplate.append(template, fromIndex, template.length()); 
} 
System.out.println(populatedTemplate); 

出力:

Hey {HH} are $=YY 

レスポンスの更新:

あなたは単語だけでなく、 ${questionNumber}ようything、あなたはこのように動的正規表現を作成する必要があります。

String template = "Hey {how} are $=you id=minScaleBox-${questionNumber}"; 
... 
map.put("${questionNumber}", "foo"); 
StringBuilder regex = new StringBuilder(); 
boolean first = true; 
for (String word : map.keySet()) { 
    if (first) { 
     first = false; 
    } else { 
     regex.append('|'); 
    } 
    regex.append(Pattern.quote(word)); 
} 
Pattern pattern = Pattern.compile(regex.toString()); 
... 

出力:

Hey {HH} are $=YY id=minScaleBox-foo 
+0

文字列=> "id = minScaleBox - $ {questionNumber}"、toreplaceString = "$ {questionNumber}"である必要があります。この場合、動作していません。それはできますか? – tarun14110

+0

レスポンスが更新されました –

+0

ありがとう、これは私が必要とするものです – tarun14110

関連する問題