2011-08-02 8 views
0

これはrichtextbox1の中の文字列の私のセットです。..C#Regex.Replace

/Category/5 
/Category/4 
/Category/19 
/Category/22 
/Category/26 
/Category/27 
/Category/24 
/Category/3 
/Category/1 
/Category/15 
http://example.org/Category/15/noneedtoadd 

私は "/" 全ての出発を変更したい "http://example.com/"

出力のようないくつかのURLを持つ:

http://example.com/Category/5 
http://example.com/Category/4 
http://example.com/Category/19 
http://example.com/Category/22 
http://example.com/Category/26 
http://example.com/Category/27 
http://example.com/Category/24 
http://example.com/Category/3 
http://example.com/Category/1 
http://example.com/Category/15 
http://example.org/Category/15/noneedtoadd 

ちょうど尋ねると、そのパターンは何ですか? :)

+3

ドライバーが必要なときにインパクトレンチを使用する理由は何ですか? –

+0

私はちょうどパターンがほしいと思っています。:) –

答えて

4

あなたがたURIを扱っているので、あなたは相対URIを解決することができますUri Classを活用することができます。

Uri baseUri = new Uri("http://example.com/"); 

Uri result1 = new Uri(baseUri, "/Category/5"); 
// result1 == {http://example.com/Category/5} 

Uri result2 = new Uri(baseUri, "http://example.org/Category/15/noneedtoadd"); 
// result2 == {http://example.org/Category/15/noneedtoadd} 
7

ここには正規表現は必要ありません。リスト内の項目を繰り返し、String.Formatを使用して目的のURLを作成します。

String.Format(@"http://example.com{0}", str); 

あなたはString.StartsWithdoc)を使用し、その後、そのテキストボックス内の項目の一つは、文字列を付加する前に、完全に形成されたURLであるかどうかを確認したい場合。

if (!String.StartsWith("http://")) { 
    // use String.Format 
} 
+0

リッチテキストボックスの中に**/**で始まっていない文字列がありますか? –

+0

それは/ category/1かcategory/1ですか? – csano

+0

サンプルの入力と出力を編集しました –

3

生の正規表現パターンは、それがでスラッシュが一致することを意味し^/ですラインの始まり。

Regex.Replace (text, @"^/", "http://example.com/") 
+0

複数行の "?m"を追加するとどうなりますか –

+1

"?m"の意味を理解できません。 RegexOptions.MultiLineを意味しますか?もしそうなら、それは私の正規表現には関係ありません。 –

関連する問題