2017-10-05 27 views
0

我試圖修改以下字符串在C#中使用System.Text.RegularExpressions.Regex,我需要用/替換/(integer)x(integer)/。例如在url中,我需要將/1080x1080/更換爲/。我怎樣才能做到這一點?我研究過正則表達式,但我無法弄清楚如何去做。使用正則表達式修改字符串

string url = "https://scontent-icn1-1.cdninstagram.com/t51.2885-15/1080x1080/22158936_1794137857546339_3682912105310191616_n.jpg"; 
//The link is actually invalid because I modified it 
+0

查看MSDN良好的參考:https://docs.microsoft。 com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference – jdweng

回答

2

可以匹配方式如下:

/\d+x\d+/ 

與替換字符串替換匹配。

在C#中,您可以在以下方式使用Regex Replace method

string output = Regex.Replace("your_string", @"/\d+x\d+/", "/"); 
+0

哦,它比我想象的要簡單得多......非常感謝! – GreenRoof

0

可以更換使用Regex.Replace字符串:

string url = "https://scontent-icn1-1.cdninstagram.com/t51.2885-15/1080x1080/22158936_1794137857546339_3682912105310191616_n.jpg"; 
url = Regex.Replace(url, @"/\d+x\d+/", "/"); 
相關問題