过滤两个特殊字符之间的文本

Filter text between two special characters

例如我有那些字符串:

"qwe/qwe/qwe/qwe//qwe/somethinghere_blabla.exe"
"qwe/qwe/q//we/qwe//qwe/somethingother_here_blabla.exe"
"qwe/qwe/qwe/qwe//qwe/some_numbers_here_blabla.exe"

现在我想获取最后一个“/”和最后一个“_”之间的文本。 所以结果将是:

"somethinghere"
"somethingother_here"
"some_numbers_here"

最简单明了的方法是什么?

我不知道该怎么做,我应该把它们分成'/'和'_',所以分开吗?我想不出任何办法。

也许从末尾开始扫描字符串,直到它到达第一个 '/' 和 '_'?还是有更简单快捷的方法?因为它必须扫描 ~10.000 个字符串。

string[] words = line.Split('/', '_'); //maybe use this? probably not

提前致谢!

string s = "qwe/qwe/q//we/qwe//qwe/somethingother_here_blabla.exe";
int last_ = s.LastIndexOf('_');
if (last_ < 0) // _ not found, take the tail of string
    last_ = s.Length;
int lastSlash = s.LastIndexOf('/');
string part = s.Substring(lastSlash + 1, last_ - lastSlash - 1);

嗯,有string.LastIndexOf:

var start = line.LastIndexOf('/') + 1;
var end = line.LastIndexOf('_');

var result = line.Substring(start, end - start);

LINQ 方式:

var str = "qwe/qwe/qwe/qwe//qwe/somethinghere_blabla.exe";
var newStr = new string(str.Reverse().SkipWhile(c => c != '_').Skip(1).TakeWhile(c => c != '/').Reverse().ToArray());
var string = "qwe/qwe/qwe/qwe//qwe/some_numbers_here_blabla.exe";
var start = string.lastIndexOf("/");
var end = string.lastIndexOf("_");
var result = string.substring(start + 1, end);

注意:如果字符串在最后一个斜杠后没有 / 或 _,则以上代码不会处理错误