2016-03-04 105 views
0

我想還是讓MP3的名稱匹配空白和不空白

我目前使用此代碼

string str = "onClick=\"playVideo('upload/honour-3.mp3',this)\"/> onClick=\"playVideo('upload/honor is my honor .mp3',this)\"/> onClick=\"playVideo('upload/honour-6.mp3',this)\"/> "; 
string Pattern = @"playVideo\(\'upload\/(?<mp3>\S*).mp3\'\,this\)"; 

if (Regex.IsMatch(str, Pattern)) 
{ 
    MatchCollection Matches = Regex.Matches(str, Pattern); 

    foreach (Match match in Matches) 
    { 
     string fn = match.Groups["mp3"].Value; 
     Debug.Log(match.Groups["mp3"].Value); 
    } 
} 

但\ S *匹配只喜歡

榮譽-3

榮譽-6

我不能得到 「的榮譽是我的榮幸」

我嘗試了「\ S * \ S *」,但它不能正常工作

我有不確定的很多多少空字符串的

如何使用正則表達式來獲得MP3的名字嗎?

+0

不會''做job..If你願意,你可以用'上傳\ /(?(\ S | \ s)*)。 mp3'代替 – rock321987

回答

1

如果你不需要匹配「playVideo」和「upload」,你的regex是不必要的複雜。這其中會產生預期的結果:

@"[\w\s-]+\.mp3" 

結果:

"honour-3.mp3", 
"honor is my honor .mp3", 
"honour-6.mp3" 

如果你不想在.mp3比賽結束,則可以在正則表達式更改爲@"([\w\s-]+)\.mp3",並選擇第二組(第一個是整場比賽)。 。

Regex.Matches(str, @"([\w\s-]+)\.mp3").Cast<Match>().Select(m => m.Groups[1].Value).ToArray(); 

結果:

"honour-3", 
"honor is my honor ", 
"honour-6" 
+0

令人驚歎!這工作! 我是指開發者,還是沒辦法理解這個原理 – user5509470

+0

謝謝!這對我有很大幫助! – user5509470

+0

@ user5509470不客氣 – Domysee