C#正则表达式-壮举。超出音频文件名称

时间:2018-09-11 13:03:00

标签: c# regex

我有一个音频文件,其名称为“ The Kemist,Nyanda ft。Braindead-Mayhem 2(Dj Reg Refix)”。现在,我想提取正则表达式的功能(是的,正则表达式,因为此示例只是多项操作的开始,而正则表达式将更加简单),因此我将获得“ Braindead”。

到目前为止,我所拥有的只是:

    public const string Feature1 = "ft?.\\s";
    public const string Feature2 = "feat?.\\s";
    public const string Feature3 = "featuring\\s";

    public const string Hyphen1 = "-";
    public const string Comma1 = ",";
    public const string And = "&";

    public const string BracketOpen1 = "(";
    public const string BracketOpen2 = "[";
    public const string BracketOpen3 = "{";

    public const string BracketClosed1 = ")";
    public const string BracketClosed2 = "]";
    public const string BracketClosed3 = "}";

    /// <summary>
    /// The words / Signs / Chars which indicate a new Artist / Feature / Title
    /// </summary>
    public static List<string> WordStopper = new List<string>()
    {
        Feature1, Feature2, Feature3,
        BracketOpen1, BracketOpen2, BracketOpen3,
        BracketClosed1, BracketClosed2, BracketClosed3,
        Hyphen1, Comma1
    };

    /// <summary>
    /// The start of a new feature
    /// </summary>
    public static List<string> FeatureBeginning = new List<string>()
    {
        Feature1,
        Feature2,
        Feature3
    };

    private static List<string> GetFeatures(string filename)
    {
        // Set the left side
        string starter = string.Join("|", FeatureBeginning.Select(w => w));

        // Set the right side
        string stopper = string.Join("|", WordStopper.Select(w => w));

        // Get the matches
        MatchCollection matches = Regex.Matches(filename, $"{starter}(\\.+){stopper}", RegexOptions.IgnoreCase);

        return null;
    }

这给了我以下错误:“ {System.ArgumentException:解析'ft?。\ s | feat?。\ s | featuring \ s(。+)ft?。\ s | feat?。\ s | featuring \ s |(| [| {| {|)|] |} |-|,'-没有足够的)。”

在这里我该怎么办?

1 个答案:

答案 0 :(得分:1)

这应该有效:

public const string Feature1 = @"ft?.\s";
public const string Feature2 = @"feat?.\s";
public const string Feature3 = @"featuring\s";

public const string Hyphen1 = "-";
public const string Comma1 = ",";
public const string And = "&";

public const string BracketOpen1 = @"\(";
public const string BracketOpen2 = @"\[";
public const string BracketOpen3 = @"\{";

public const string BracketClosed1 = @"\)";
public const string BracketClosed2 = @"\]";
public const string BracketClosed3 = @"\}";

/// <summary>
/// The words / Signs / Chars which indicate a new Artist / Feature / Title
/// </summary>
public static List<string> WordStopper = new List<string>()
{
    Feature1, Feature2, Feature3,
    BracketOpen1, BracketOpen2, BracketOpen3,
    BracketClosed1, BracketClosed2, BracketClosed3,
    Hyphen1, Comma1
};

/// <summary>
/// The start of a new feature
/// </summary>
public static List<string> FeatureBeginning = new List<string>()
{
    Feature1,
    Feature2,
    Feature3
};

public static List<string> GetFeatures(string filename)
{
    // Set the left side
    string starter = "(" + string.Join(")|(", FeatureBeginning.ToArray()) + ")";

    // Set the right side
    string stopper = "(" + string.Join(")|(", WordStopper.ToArray()) + ")";

    // Get the matches
    MatchCollection matches = Regex.Matches(filename, "(?<=(" + starter + "))(.+?)(?=(" + stopper + "))", RegexOptions.IgnoreCase | RegexOptions.Singleline);

    return null;
}

您必须检查一些未逃脱的表达式。另外,您需要匹配所有内容,直到最后一个塞子,直到第一个塞子。

相关问题