C# 正则表达式提取内容

C# 正则表达式提取内容,c#,.net,regex,C#,.net,Regex,我不知道如何在c#中使用正则表达式。我很困惑。 以下是我要在其中获取id的URL: 3858715.jpg 我只想得到粗体的数字 我尝试使用的正则表达式是: Match thumb_id = Regex.Match(url, @"\/(?)\.jpg"); 怎么了 有什么帮助吗?请使用此选项: Match thumb_id = Regex.Match(url, "http://(\\S+?)\\.(jpg)"); 所以你有: foreach (Match m in Regex.Match

我不知道如何在c#中使用正则表达式。我很困惑。 以下是我要在其中获取id的URL:

3858715.jpg

我只想得到粗体的数字

我尝试使用的正则表达式是:

Match thumb_id = Regex.Match(url, @"\/(?)\.jpg");
怎么了

有什么帮助吗?

请使用此选项:

Match thumb_id = Regex.Match(url, "http://(\\S+?)\\.(jpg)");
所以你有:

  foreach (Match m in Regex.Matches(s, "http://(\\S+?)\\.(jpg)"))
        {
            Console.WriteLine(m.Groups[1].Value);
        }
改用这个:

Match thumb_id = Regex.Match(url, "http://(\\S+?)\\.(jpg)");
所以你有:

  foreach (Match m in Regex.Matches(s, "http://(\\S+?)\\.(jpg)"))
        {
            Console.WriteLine(m.Groups[1].Value);
        }

您可以在不使用正则表达式的情况下执行此操作:

url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));

您可以在不使用正则表达式的情况下执行此操作:

url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));

它是一个url。你不需要正则表达式

var url = "https://j-ec.static.com/images/385/3858715.jpg";

var id = Path.GetFileNameWithoutExtension(url);

它是一个url。你不需要正则表达式

var url = "https://j-ec.static.com/images/385/3858715.jpg";

var id = Path.GetFileNameWithoutExtension(url);

匹配零个或它前面的一个。在你的正则表达式中,前面没有任何内容。你没有说你想匹配什么,但可能是一个或多个数字:
@“\/([0-9]+)\.jpg”)
那么
[0-9]+(?=.jpg)
呢?当您刚接触regex时,我建议您使用regexstorm.net,在这里您可以轻松地试验regex表达式并在您的输入上测试它们。
匹配零或之前的一个表达式。在你的正则表达式中,前面没有任何内容。你没有说你想匹配什么,但可能是一个或多个数字:
@“\/([0-9]+)\.jpg”)
那么
[0-9]+(?=.jpg)
呢?当您刚接触regex时,我建议您使用regexstorm.net,在这里您可以轻松地试验regex表达式并在输入上测试它们。