匹配字符串模式和数字的正则表达式(url 格式)
Regex matching a string pattern and number ( url format )
我有一个遵循 url 模式的字符串
https://www.examples.org/activity-group/8712/activity/202803
// note : the end ending of the url can be different
https://www.examples.org/activity-group/8712/activity/202803?ref=bla
https://www.examples.org/activity-group/8712/activity/202803/something
我正在尝试编写匹配
的正则表达式
https://www.examples.org/activity-group/{number}/activity/{number}*
其中 {number} 是长度为 1 到 10 的整数。
如何定义一个正则表达式来检查字符串模式并检查数字是否在字符串中的正确位置?
背景:在 Google 表格中,为了验证答案,我想强制人们以这种格式输入 url。因此使用这个正则表达式。
对于不符合该格式的网址,正则表达式应 return 为假。例如:https://www.notthesite.org/group/8712/activity/astring
我举了几个例子,但它们只有在数字出现在字符串中时才匹配。
示例来源:
- How to find a number in a string using JavaScript?
- Get the first Int(s) in a string with javascript
^https:\/\/www\.examples\.org\/activity-group\/[0-9]{1,10}\/activity\/[0-9]{1,10}(\/[a-z]+)*((\?[a-z]+=[a-zA-Z0-9]+)(\&[a-z]+=[a-zA-Z0-9]+)*)*$
^
- 字符串开头
\
- 转义字符
[0-9]
- 一个数字
{1,10}
- 在前面的项目中的一到十之间
(\/[a-z]+)*
- 允许额外的 URL 段
((\?[a-z]+=[a-zA-Z0-9]+)(\&[a-z]+=[a-zA-Z0-9]+)*)*
- 允许第一个参数使用 ?
的查询参数,所有其他参数使用 &
$
- 字符串结尾
这是假设 URL 段和查询参数键仅为小写字母。查询参数值可以是小写字母、大写字母或数字。
你可以使用
https?:\/\/(?:[^/]+\/){2}(\d+)\/[^/]+\/(\d+)
分解后,这表示:
https?:\/\/ # http:// or https://
(?:[^/]+\/){2} # not "/", followed by "/", twice
(\d+) # 1+ digits
\/[^/]+\/ # same pattern as above
(\d+) # the other number
您需要分别使用组 1
和 2
。
如果这太宽松,请使用
https:\/\/[^/]+\/activity-group\/(\d+)\/activity\/(\d+)
上面写着
https:\/\/[^/]+ # https:// + some domain name
\/activity-group\/ # /activity-group/
(\d+) # first number
\/activity\/ # /activity/
(\d+) # second number
再看一个demo on regex101.com。
可能你需要这样的东西:
(http[s]?:\/\/)?www.examples.org\/activity-group\/(\d{1,10})\/activity\/(\d{1,10})([\S]+?)$
其中:
(http[s]?:\/\/)?
匹配任何 http:// 或 https:// 部分。
www.examples.org
是您的域名。
(\d{1,10})
将匹配最大长度为 10 的第一个整数(在 activity-group 之后)。
- 第二个
(\d{1,10})
将匹配activity. 之后的第二个整数
- 最后
([\S]+?)$
将匹配第二个数字之后的任何可选数据,直到找到新行为止,假设您将多行标志与 \m
. 一起使用
查看
希望对您有所帮助!
我有一个遵循 url 模式的字符串
https://www.examples.org/activity-group/8712/activity/202803
// note : the end ending of the url can be different
https://www.examples.org/activity-group/8712/activity/202803?ref=bla
https://www.examples.org/activity-group/8712/activity/202803/something
我正在尝试编写匹配
的正则表达式https://www.examples.org/activity-group/{number}/activity/{number}*
其中 {number} 是长度为 1 到 10 的整数。
如何定义一个正则表达式来检查字符串模式并检查数字是否在字符串中的正确位置?
背景:在 Google 表格中,为了验证答案,我想强制人们以这种格式输入 url。因此使用这个正则表达式。
对于不符合该格式的网址,正则表达式应 return 为假。例如:https://www.notthesite.org/group/8712/activity/astring
我举了几个例子,但它们只有在数字出现在字符串中时才匹配。
示例来源:
- How to find a number in a string using JavaScript?
- Get the first Int(s) in a string with javascript
^https:\/\/www\.examples\.org\/activity-group\/[0-9]{1,10}\/activity\/[0-9]{1,10}(\/[a-z]+)*((\?[a-z]+=[a-zA-Z0-9]+)(\&[a-z]+=[a-zA-Z0-9]+)*)*$
^
- 字符串开头\
- 转义字符[0-9]
- 一个数字{1,10}
- 在前面的项目中的一到十之间(\/[a-z]+)*
- 允许额外的 URL 段((\?[a-z]+=[a-zA-Z0-9]+)(\&[a-z]+=[a-zA-Z0-9]+)*)*
- 允许第一个参数使用?
的查询参数,所有其他参数使用&
$
- 字符串结尾
这是假设 URL 段和查询参数键仅为小写字母。查询参数值可以是小写字母、大写字母或数字。
你可以使用
https?:\/\/(?:[^/]+\/){2}(\d+)\/[^/]+\/(\d+)
分解后,这表示:
https?:\/\/ # http:// or https://
(?:[^/]+\/){2} # not "/", followed by "/", twice
(\d+) # 1+ digits
\/[^/]+\/ # same pattern as above
(\d+) # the other number
您需要分别使用组 1
和 2
。
如果这太宽松,请使用
https:\/\/[^/]+\/activity-group\/(\d+)\/activity\/(\d+)
上面写着
https:\/\/[^/]+ # https:// + some domain name
\/activity-group\/ # /activity-group/
(\d+) # first number
\/activity\/ # /activity/
(\d+) # second number
再看一个demo on regex101.com。
可能你需要这样的东西:
(http[s]?:\/\/)?www.examples.org\/activity-group\/(\d{1,10})\/activity\/(\d{1,10})([\S]+?)$
其中:
(http[s]?:\/\/)?
匹配任何 http:// 或 https:// 部分。www.examples.org
是您的域名。(\d{1,10})
将匹配最大长度为 10 的第一个整数(在 activity-group 之后)。- 第二个
(\d{1,10})
将匹配activity. 之后的第二个整数
- 最后
([\S]+?)$
将匹配第二个数字之后的任何可选数据,直到找到新行为止,假设您将多行标志与\m
. 一起使用
希望对您有所帮助!