C# 获取字符串的数字部分

C# 获取字符串的数字部分,c#,regex,string,C#,Regex,String,我有一个类似“1000/Refuse5.jpg”或“50/Refuse5.jpeg”的字符串。 请注意,本例1000或50中字符串的第一部分是可变的。 我想通过C#方法从这个字符串中得到“5”数字。 有人能帮我吗?你可以用正则表达式 string input = "1000/Refuse5.jpg"; var num = Regex.Matches(input, @"\d+").Cast<Match>().Last().Value; string input=“1000/Refus

我有一个类似“1000/Refuse5.jpg”或“50/Refuse5.jpeg”的字符串。 请注意,本例1000或50中字符串的第一部分是可变的。 我想通过C#方法从这个字符串中得到“5”数字。 有人能帮我吗?

你可以用正则表达式

string input = "1000/Refuse5.jpg";
var num = Regex.Matches(input, @"\d+").Cast<Match>().Last().Value;
string input=“1000/Refuse5.jpg”;
var num=Regex.Matches(输入@“\d+”).Cast().Last().Value;
您可以使用正则表达式

string input = "1000/Refuse5.jpg";
var num = Regex.Matches(input, @"\d+").Cast<Match>().Last().Value;
string input=“1000/Refuse5.jpg”;
var num=Regex.Matches(输入@“\d+”).Cast().Last().Value;

一个更受约束的正则表达式

var fileName = "1000/Refuse5.jpg";
var match = Regex.Match(fileName, @"(?<=\D+)(\d+)(?=\.)");

if(match.Success)
{
    var value = int.Parse(match.Value);
}
var fileName=“1000/Refuse5.jpg”;

var match=Regex.match(文件名,@)(?一个更受约束的正则表达式

var fileName = "1000/Refuse5.jpg";
var match = Regex.Match(fileName, @"(?<=\D+)(\d+)(?=\.)");

if(match.Success)
{
    var value = int.Parse(match.Value);
}
var fileName=“1000/Refuse5.jpg”;

var match=Regex.match(fileName,@)(?您可以使用正则表达式提取字符串的相关部分,然后将其转换为整数。您需要研究输入集,并确保使用的正则表达式符合您的需要

        string input = "1234/Refuse123.jpg";

        // Look for any non / characters until you hit a /
        // then match any characters other than digits as many
        // as possible. After that, match digits as many as possible
        // and capture them in a group (hence the paranthesis). And 
        // finally match everything else at the end of the string
        Regex regex = new Regex("[^/]*/[^\\d]*([\\d]*).*");

        var match = regex.Match(input);

        // Group 0 will be the input string
        // Group 1 will be the captured numbers
        Console.WriteLine(match.Groups[1]);

您可以使用正则表达式提取字符串的相关部分,然后将其转换为整数。您需要研究输入集,并确保使用的正则表达式符合您的需要

        string input = "1234/Refuse123.jpg";

        // Look for any non / characters until you hit a /
        // then match any characters other than digits as many
        // as possible. After that, match digits as many as possible
        // and capture them in a group (hence the paranthesis). And 
        // finally match everything else at the end of the string
        Regex regex = new Regex("[^/]*/[^\\d]*([\\d]*).*");

        var match = regex.Match(input);

        // Group 0 will be the input string
        // Group 1 will be the captured numbers
        Console.WriteLine(match.Groups[1]);
更干净的正则表达式:

Console.WriteLine (Regex.Match("123ABC5", @"\d", RegexOptions.RightToLeft).Value); // 5
注意,如果最后一个数字超过一位,请使用
\d+

更干净的正则表达式:

Console.WriteLine (Regex.Match("123ABC5", @"\d", RegexOptions.RightToLeft).Value); // 5

注意,如果最后一个数字将超过一位,请使用
\d+

使用子字符串函数获取“/”字符之前的所有内容,然后将其转换。使用子字符串函数获取“/”字符之前的所有内容字符,然后将其转换。忘记取出特定于linqpad的内容。忘记取出特定于linqpad的内容。当您可以让正则表达式解析器只查看最后一项而不处理整个字符串时,为什么会有Last()的开销?请参阅我的文章。为什么会有Last()的开销当您可以让正则表达式解析器只查看最后一项,而不处理整个字符串时,请参阅我的post.rightoleft-好主意。rightoleft-好主意。