Regex 用于筛选URL的正则表达式
如何编写正则表达式并区分顶级URL和顶级URL内链接的bRegex 用于筛选URL的正则表达式,regex,Regex,如何编写正则表达式并区分顶级URL和顶级URL内链接的b For e.g, if the top level url is http://www.example.com/ and other links inside this top folder can be, http://www.example.com/go http://www.example.com/contact/ http://www.example.com/links/ 我不知道顶部文件夹中有什么链接,是否有一个正则表达式
For e.g, if the top level url is http://www.example.com/
and other links inside this top folder can be,
http://www.example.com/go
http://www.example.com/contact/
http://www.example.com/links/
我不知道顶部文件夹中有什么链接,是否有一个正则表达式可以选择主文件夹以及主文件夹中的所有子文件夹
谢谢。我建议从一个正则表达式开始,将url分解为其组件。有很多例子。这本书摘自《Regex食谱》的作者简·戈瓦茨(Jan Goyvaerts): URL的不同段在中的不同捕获组中可用,请查看右侧窗格中的组 然后,如果要匹配较少的组件,请缩短正则表达式:
^(?im)\b(?<protocol>https?|ftp)://(?<domain>[-A-Z0-9.]+)/?$
请参见如何在没有文件的情况下匹配url。因为您不想验证url,所以只需从索引1顶级url和2任何后跟顶级url的内容中获取匹配的组,该顶级url通过将其括在括号内捕获 这里是,点击代码生成器链接以获得所需语言的代码 模式说明:
^ the beginning of the string
http: 'http:'
\/ '/'
\/ '/'
( group and capture to \1:
[^\/]* any character except: '\/' (0 or more times (Greedy))
) end of \1
\/ '/'
( group and capture to \2:
.* any character except \n (0 or more times (Greedy))
) end of \2
$ before an optional \n, and the end of the string
如果URL位于多行中的字符串或跨度内,则使用以下正则表达式:
\bhttp:\/\/([^\/]*)\/([^\s]*)
是另一个URL。请查看是否也要验证该URL?不,我不想验证它们。仅供参考,我为较短的URL添加了第二个演示。
^ the beginning of the string
http: 'http:'
\/ '/'
\/ '/'
( group and capture to \1:
[^\/]* any character except: '\/' (0 or more times (Greedy))
) end of \1
\/ '/'
( group and capture to \2:
.* any character except \n (0 or more times (Greedy))
) end of \2
$ before an optional \n, and the end of the string
\bhttp:\/\/([^\/]*)\/([^\s]*)