2017-01-09 5 views
0

"{name_of_placeholder}"のようなプレースホルダを含む行が多数あるテキストファイルから読み込みます。地図のような別のファイルがあります - キーは各プレースホルダの名前であり、それぞれの値があります。正規表現を使用して最初のファイルのすべてのプレースホルダを探し、{name_of_placeholder}を2番目のファイルの対応する値に置き換えます。キャプチャしたグループを正規表現の文字列の外で再利用

私の頭に浮かんだ最初のことは、「{}」の間にグループをキャプチャすることですが、文字列の外側でそれを使用する方法はありますか?それが可能でない場合、誰かがこれを行う別の方法を考えることができるかもしれません?

ありがとうございます!

+0

1)あなたはどのような言語を使用していますか? 2)第2ファイルの正確な形式は何ですか3)ファイル1とファイル2から適切な入力行を与えてください –

+0

1)この置き換えをプレースホルダの代わりにpowershellスクリプトで実行したいと思います。 2)2番目のファイルは、base64でエンコードされた文字列を含むXMLです。 3)最初のファイルのものです:get {return "{ApplicationTitle}"; } 2番目から: cmFuZG9taW5wdXRsaW5l

答えて

0

あなたが言語を定義していないが、どんな言語である、あなたは次のようなアプローチしようとすることができますが:

var dict={} 
 
const regex1 = /(.*)=(.*)/gm; 
 

 
// let str1 be the second file (dictionary) 
 
const str1 = `abc1=1 
 
abc2=2 
 
abc3=3 
 
abc4=4 
 
abc5=5 
 
abc6=6 
 
abc7=7 
 
abc8=8 
 
abc9=9 
 
abc10=10 
 
abc11=11 
 
abc12=12`; 
 
let m1; 
 

 
while ((m1 = regex1.exec(str1)) !== null) { 
 
    if (m1.index === regex1.lastIndex) { 
 
     regex1.lastIndex++; 
 
    } 
 
    dict[m1[1]]=m1[2]; 
 
} 
 
//console.log(dict); 
 

 

 

 
const regex = /\{(.*?)\}/gm; 
 
// let str be the first file where you want the replace operation on {key...} 
 
var str = `adfas{abc1} asfasdf 
 
asdf {abc3} asdfasdf 
 
asdfas {abc5} asdfasdf 
 
asdfas{abc7} asdfasdfadf 
 
piq asdfj asdf 
 
`; 
 
let m; 
 

 
while ((m = regex.exec(str)) !== null) { 
 
    if (m.index === regex.lastIndex) { 
 
     regex.lastIndex++; 
 
    } 
 
     str=str.replace("\{"+m[1]+"\}",dict[m[1]]); 
 
    
 
} 
 
console.log(str);