Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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/19.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中使用正则表达式提取数据#_C#_Regex - Fatal编程技术网

C# 在C中使用正则表达式提取数据#

C# 在C中使用正则表达式提取数据#,c#,regex,C#,Regex,在C#中,如何从该字符串中提取信息: http://www.example.com/images/this-is-the-url-1234-12-filename.jpg 使用正则表达式,以便获得以下数据: /1234/12/filename.jpg 上面只是URL模式的一个示例。以下是更多的几个例子: http://www.example1.com/images/this-is-something-else-5555-10-anotherfilename.jpg 应返回: /5555/1

在C#中,如何从该字符串中提取信息:

http://www.example.com/images/this-is-the-url-1234-12-filename.jpg
使用正则表达式,以便获得以下数据:

/1234/12/filename.jpg
上面只是URL模式的一个示例。以下是更多的几个例子:

http://www.example1.com/images/this-is-something-else-5555-10-anotherfilename.jpg
应返回:

/5555/10/anotherfilename.jpg

非常感谢您的帮助。谢谢。

根据提供的两个示例URL,您可以使用以下模式提取所需信息:

string pattern = @"-(\d+\-\d+\-[a-zA-Z]+\.[a-zA-Z]+)"; 
使用以下代码将URL拆分为两部分,其中所需字符串位于第二个子字符串中:

string input = "http://www.example.com/images/this-is-the-url-1234-12-filename.jpg";
string pattern = @"-(\d+\-\d+\-[a-zA-Z]+\.[a-zA-Z]+)";           

string[] substrings = Regex.Split(input, pattern);
因此
子字符串[1]
将是
1234-12-filename.jpg
。那么您只需要将“-”替换为“/”,例如:

var newString = "/" + substrings[1].Replace("-", "/")
你应该得到想要的结果

注意:这仅在您希望更改的所有URL最终都具有以下模式时才有效:

“-数字文件名。扩展名”


而且这种模式不会出现在URL中的任何其他地方

张贴你的照片directly@ADITYA我认为链接的目的不是图像,它们只是输入数据的示例