解析字符串以提取URL或文件夹路径 [英] Parsing a string to extract a URL or folder path

查看:346
本文介绍了解析字符串以提取URL或文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了查看杜高拱门(Dour High Arch)的评论,他说:

I asked a similar question recently about using regex to retrieve a URL or folder path from a string. I was looking at this comment by Dour High Arch, where he says:

我建议您根本不使用正则表达式;使用单独的代码路径 URL,使用Uri类,以及文件路径,使用FileInfo 班级.这些类已经处理解析,匹配,提取 组件等等."

"I recommend you do not use regexes at all; use separate code paths for URLs, using the Uri class, and file paths, using the FileInfo class. These classes already handle parsing, matching, extracting components, and so on."

我从没有真正尝试过,但是现在我正在研究它,无法弄清楚他所说的内容对我要完成的工作是否真的有用.

I never really tried this, but now I am looking into it and can't figure out if what he said actually is useful to what I'm trying to accomplish.

我希望能够解析一个类似于以下内容的字符串消息:

I want to be able to parse a string message that could be something like:

我将文件放在服务器上的 http://www.thewebsite.com/NewStuff,他们也可以 可以通过J:\ Downloads \ NewStuff在本地网络驱动器上访问"

"I placed the files on the server at http://www.thewebsite.com/NewStuff, they can also be reached on your local network drives at J:\Downloads\NewStuff"

并提取两个字符串http://www.thewebsite.com/J:\Downloads\NewStuff.我没有看到UriFileInfo类上的任何方法来解析字符串中的UriFileInfo对象,就像我认为Dour High Arch所暗示的那样.

And extract out the two strings http://www.thewebsite.com/ and J:\Downloads\NewStuff. I don't see any methods on the Uri or FileInfo class that parse a Uri or FileInfo object from a string like I think Dour High Arch was implying.

使用UriFileInfo类是否缺少某些允许这种行为的东西?如果不是,那么框架中是否还有其他类可以做到这一点?

Is there something I'm missing about using the Uri or FileInfo class that will allow this behavior? If not is there some other class in the framework that does this?

推荐答案

U可以使用:

(?<type>[^ ]+?:)(?<path>//[^ ]*|\\.+\\[^ ]*)

每个结果将为您提供2组

that will give you 2 groups on each result

类型:"http:"

路径://www.thewebsite.com/NewStuff

类型:"J:"

路径:\Downloads\NewStuff

超出字符串

我将文件放在服务器上的 http://www.thewebsite.com/NewStuff ,也可以通过您的网站访问它们 本地网络驱动器位于J:\ Downloads \ NewStuff"

"I placed the files on the server at http://www.thewebsite.com/NewStuff, they can also be reached on your local network drives at J:\Downloads\NewStuff"

您可以使用类型"组来查看类型是否为http:并对此进行操作.

you can use the "type" group to see if the type is http:or not and set action on that.

编辑

或如果您确定文件路径中没有空格,请在下面使用正则表达式:

or use regex below if you are sure there is no whitespace in your filepath :

(?<type>[^ ]+?:)(?<path>//[^ ]*|\\[^ ]*)

这篇关于解析字符串以提取URL或文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆