Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#正则表达式-从末端切下,但带有扭曲_C#_Regex_Windows Runtime - Fatal编程技术网

C#正则表达式-从末端切下,但带有扭曲

C#正则表达式-从末端切下,但带有扭曲,c#,regex,windows-runtime,C#,Regex,Windows Runtime,我有一个字符串列表,我必须迭代并从末尾删除一些内容。字符串通常如下所示: String first = "Something here (SMM)"; String second = "Something else (&)"; String third = "Something here (1922) (IJK)"; 我想做的是,去掉最后一对括号,不管它们的内容是什么。说到正则表达式,我是个笨蛋,所以我愿意接受关于如何解决这个特殊问题的建议 编辑: 忘记了我想要的结果: String

我有一个字符串列表,我必须迭代并从末尾删除一些内容。字符串通常如下所示:

String first = "Something here (SMM)";
String second = "Something else (&)";
String third = "Something here (1922) (IJK)";
我想做的是,去掉最后一对括号,不管它们的内容是什么。说到正则表达式,我是个笨蛋,所以我愿意接受关于如何解决这个特殊问题的建议

编辑:

忘记了我想要的结果:

String firstFinal = "Something here";
String secondFinal = "Something else";
String thirdFinal = "Something here (1922)";

您不能创建一个方法,将字符串传递到该方法中以获得输出吗

    string first = stripped("Something here (SMM)");

    public string stripped(string stripper)
    {
        int indexOfFirst = stripper.LastIndexOf('(');
        int indexOfSecond = stripper.LastIndexOf(')');
        string output = stripper.Remove(indexOfFirst, (indexOfSecond - indexOfFirst) + 1);
        return output;
    }

您不能创建一个方法,将字符串传递到该方法中以获得输出吗

    string first = stripped("Something here (SMM)");

    public string stripped(string stripper)
    {
        int indexOfFirst = stripper.LastIndexOf('(');
        int indexOfSecond = stripper.LastIndexOf(')');
        string output = stripper.Remove(indexOfFirst, (indexOfSecond - indexOfFirst) + 1);
        return output;
    }

以下模式应满足您的要求:

\s\([^)]*\)$
分解如下:

\s
匹配最后一个必需字符和左括号之间的空白

\(
是一个文字括号

[^)]*
是任何不是

\)
是一个文字右括号

$
是字符串的结尾

此代码

string pattern = @"\s\([^)]*\)$";

String first = "Something here (SMM)";
String second = "Something else (&)";
String third = "Something here (1922) (IJK)";

first = Regex.Replace(first, pattern, "");
second = Regex.Replace(second, pattern, "");
third = Regex.Replace(third, pattern, "");

Console.WriteLine(first);
Console.WriteLine(second);
Console.WriteLine(third);
将打印

这里有东西

别的

这里的某物(1922)


以下模式应满足您的要求:

\s\([^)]*\)$
分解如下:

\s
匹配最后一个必需字符和左括号之间的空白

\(
是一个文字括号

[^)]*
是任何不是

\)
是一个文字右括号

$
是字符串的结尾

此代码

string pattern = @"\s\([^)]*\)$";

String first = "Something here (SMM)";
String second = "Something else (&)";
String third = "Something here (1922) (IJK)";

first = Regex.Replace(first, pattern, "");
second = Regex.Replace(second, pattern, "");
third = Regex.Replace(third, pattern, "");

Console.WriteLine(first);
Console.WriteLine(second);
Console.WriteLine(third);
将打印

这里有东西

别的

这里的某物(1922)


所以第三个应该是这里的某个东西(1922年)?确切地说。我将添加结果模式。因此,
第三个
应该是
这里的东西(1922)
?确切地说。我将添加结果模式。我一直称“[”a括号”()为parenthesis@petelids-如果第三个字符串是“某物在这里(1922)(IJK)测试”怎么办?我认为正则表达式不能正常工作。@Ntellect13-你说得很对,它会让字符串保持原样。OP不清楚在这种情况下会发生什么。@reggaeguitar-在英国,我们通常称之为
方括号内加
[
]
是方括号。但是,我怀疑更多的读者会使用括号,所以我更新了答案:)。我一直称“[”为括号”()为括号parenthesis@petelids-如果第三个字符串是“某物在这里(1922)(IJK)测试”怎么办?我认为正则表达式不能正常工作。@Ntellect13-你说得很对,它会让字符串保持原样。OP不清楚在这种情况下会发生什么。@reggaeguitar-在英国,我们通常称之为
方括号内加
[
]
是方括号。但是,我怀疑更多的读者会使用括号,所以我更新了答案:)。