与空白匹配,没有空白

时间:2016-03-04 05:58:17

标签: c# regex

我想要或得到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

我无法获得&#34;荣誉是我的荣幸&#34;

我尝试了&#34; \ S * \ s *&#34;但是它不起作用

我有很多空白字符串不确定

如何使用Regex获取mp3的名称?

1 个答案:

答案 0 :(得分:1)

如果您不必匹配“playVideo”和“upload”,那么您的正则表达式会不必要地复杂化。这个产生了预期的结果:

@"[\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"
相关问题