2017-05-12 10 views
0

groovyscriptを使用していくつかのURLを操作しようとしています。 FOLDERは、別のフォルダ名のリストの一つであるURL、groovyscriptを操作するためのマッピング関数

http://wiki.somecompany.com/FOLDER/file/attach/FOLDER/test/random.txt 

URLは形式で出力されました。

これらのURLは、実際に変換する必要があり、以下:私はこのコードで一度に一つのフォルダを変更することができます

http://wiki.somecompany.com/pub/FOLDER/test/random.txt 

def longFOLDERName = "FOLDER/file/attach/FOLDER"; 
def FOLDERName = "pub/FOLDER"; 

displayURL = url.replaceAll(longFOLDERName,FOLDERName); 

がそれぞれ別のフォルダ名のためにそれを繰り返すが、明らかにこれは時間がかかり、非効率的です。

.com//fileの間でテキストを選択し、添付ファイル/の後のフォルダ名と比較してから、すべてを関数に変換しますか?

URLの文字列値は、displayURLとして格納されます。

+0

なぜ文字列を比較したいですか?その後、文字列を置き換えることになりますか?私が言いたいのは、 '' replaceAll''関数はあなた自身の実装よりもあまり良くありません – dsharew

答えて

0

私はあなたが正規表現で行くことができると信じています。

def url = 'http://wiki.somecompany.com/MY_FOLDER/file/attach/MY_FOLDER/test/random.txt' 
def regex = /^http:\/\/wiki\.somecompany\.com\/(.+)\/file\/attach\/(.+)\/test\/random\.txt$/ 

def matcher = (url =~ regex) 
def folder = matcher[0][1] // here you get your "MY_FOLDER" 

def resultUrl = "http://wiki.somecompany.com/pub/$folder/test/random.txt" // here you make string interpolation 

お気づきのように、(.+)は任意の文字列に一致します。 [0]は一致するリスト([http://wiki.somecompany.com/MY_FOLDER/file/attach/MY_FOLDER/test/random.txt, MY_FOLDER, MY_FOLDER])を取得し、[1]は気にするフォルダ名です。

は、それはあなただけFOLDER以上のものをキャプチャするために正規表現を使用することができます

0
def stringInput = "http://wiki.somecompany.com/FOLDER/file/attach/FOLDER/test/random.txt"; 
def stringSplit = stringInput.split("/"); 

i=0; 
while (stringSplit[i] != "attach"){ 
i++; 
} 


if (i+1 != stringSplit.length && stringSplit[i-2] ==stringSplit[i+1] && stringSplit[i-1]=="file"){ 
def stringOutput = stringInput.replaceAll(stringSplit[i+1]+"/file/attach","pub"); 
} 
0

に役立ちます願っています。

def urls = [ 
"http://wiki.somecompany.com/FOLDER/file/attach/FOLDER/test/random.txt", 
"http://wiki.somecompany.com/OTHER/file/attach/OTHER/something/random.html", 
] 

def r = /(FOLDER|OTHER)\/file\/attach\/\1/ 
//  ^   ^   ^
//  |    |    +-- backref first group 
//  |    +-- Stuff in between you want get rid off 
//  +-- All folder names, you want to change 

assert [ 
"http://wiki.somecompany.com/pub/FOLDER/test/random.txt", 
"http://wiki.somecompany.com/pub/OTHER/something/random.html", 
] == urls.collect{ 
    it.replaceAll(r, 'pub/$1') // replace the whole match with `/pub/` 
           // and the value of the first group 
} 
関連する問題