首页 > 解决方案 > 正则表达式网站只有一个子文件夹,之后没有别的

问题描述

任何人都可以帮助正确的正则表达式模式吗?基本上我想捕获所有只有一个子文件夹的字符串,之后没有其他内容(可能是正斜杠除外)。

这是我的正则表达式,但它并不匹配所有内容:

Regex Pattern: http(s)?:\/\/(.*).(.*)/(\w-)*\b

要匹配的字符串(我想匹配的是箭头):

    http://test.org/
==> http://test.org/SubFolder1             
    http://test.org/SubFolder1?Query=Test
    http://test.org/SubFolder1/SubFolder2
    http://test.org/SubFolder1/SubFolder2?Query=Test
    http://www.test.org/
==> http://www.test.org/SubFolder1  
    http://www.test.org/SubFolder1?Query=Test
    http://www.org/SubFolder1/SubFolder2
    http://www.org/SubFolder1/SubFolder2?Query=Test
    www.test.org/
==> www.test.org/SubFolder1  
    www.test.org/SubFolder1?Query=Test
    www.org/SubFolder1/SubFolder2
    www.org/SubFolder1/SubFolder2?Query=Test

提前致谢。

标签: c#regexurl

解决方案


使用 regexr 我能够解决问题。很多时候,您无法通过谷歌搜索您的确切解决方案,因此您应该花一些时间尝试了解如何为您的独特需求编写正则表达式。

(https?:\/\/)?\w+\.+[\w\.]*\/[\w-]+$
  • 可选 https
  • 1 个或多个字母
  • 1 个或多个点
  • 任意数量的字母或点
  • 一个斜线(我在这里逃脱了它,你没有逃脱它 - 有时它需要)
  • 1 个或多个字母或连字符(您的字符中有连字符)
  • 行结束

我在这里创建了一个正则表达式,它以非常图形化的方式解释了解决方案。


推荐阅读