我有我需要轉換爲URL的以下路徑。使用正則表達式在字符串中匹配多個字符
string path = @"\\TestServer\User_Attachments$\Data\Reference\Input\Test.png";
我試圖替換替換此字符串中的特殊字符。所以
\\ will become
//with an
HTTPSadded at the front i.e.
https://開頭`\
將成爲/
User_Attachments$
將成爲User_Attachments
最終的字符串應該像
string url = "https://TestServer/User_Attachments/Data/Reference/Input/Test.png"
要做到這一點,我想出了以下regex
string pattern = @"^(.{2})|(\\{1})|(\${1})";
我再搭配使用Matches()
方法:
var match = Regex.Matches(path, pattern);
我的問題是我怎麼能檢查,看看是否匹配是成功並在相應的組中取代適當的值,然後如上所述獲得最終的url
字符串。
Here是鏈接到正則表達式
你爲什麼要用正則表達式來做呢?一個簡單的'string.Replace()'鏈接就足夠了,並允許更好的可讀性的最終代碼... – zaitsman
@zaitsman我不必使用正則表達式,我只是不知道如何使用字符串替換多個值.Replace()'因此我沿着這條路線走了。如果你可以提供一個例子 – Code