c# - 正则表达式网站只有一个子文件夹,之后没有别的
问题描述
任何人都可以帮助正确的正则表达式模式吗?基本上我想捕获所有只有一个子文件夹的字符串,之后没有其他内容(可能是正斜杠除外)。
这是我的正则表达式,但它并不匹配所有内容:
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
提前致谢。
解决方案
使用 regexr 我能够解决问题。很多时候,您无法通过谷歌搜索您的确切解决方案,因此您应该花一些时间尝试了解如何为您的独特需求编写正则表达式。
(https?:\/\/)?\w+\.+[\w\.]*\/[\w-]+$
- 可选 https
- 1 个或多个字母
- 1 个或多个点
- 任意数量的字母或点
- 一个斜线(我在这里逃脱了它,你没有逃脱它 - 有时它需要)
- 1 个或多个字母或连字符(您的字符中有连字符)
- 行结束
我在这里创建了一个正则表达式,它以非常图形化的方式解释了解决方案。
推荐阅读
- c - 如何反转2的分数幂的定点二进制对数算法?
- oracle - 我正在尝试使用异构数据库连接从 Oracle 连接到其他数据库,如 PostgreSQL
- php - 如何在laravel中使用父目录中的控制器
- java - 使用机器人类 Java 按键特殊字符
- xml - 从txt文件中提取xml
- javascript - 仅使用 Jquery 将链接分配给图像并使其可点击
- java - 在 IntelliJ IDEA 2020.1 中重命名模块时出错?
- vector - 对rust中的向量子集执行操作?
- altair - 在 Altair 中绘制指示分组堆叠条形图净值的标记
- flutter - 如何使用 Provider 为 PageView() 提供一个块,而无需每次切换页面时孩子都重新订阅?