Regex to match a fully qualified url exactly like https://merchant.com while in place of merchant it can be any letter

huangapple go评论145阅读模式
英文:

Regex to match a fully qualified url exactly like https://merchant.com while in place of merchant it can be any letter

问题

我需要一个用于URL https://example.com 的正则表达式,而在 example 的位置可以是任何字母。

我尝试过 https:\/\/[a-z]\.[a-z],但它只接受了 example 中的第一个字母和 com 中的第一个字母,即 https://e.c(使用上述正则表达式接受),但我希望它能够接受问题中提到的整个URL(https://example.com)。

英文:

I need a regex for the url https://example.com while in place of example it can be any letter

I tried https:\/\/[a-z]\.[a-z] but is only accepting the first letter in example and first letter in com which is https://e.c (accepting this with above regex) but I want it to accept whole url(https://example.com) as mentioned in the question.

答案1

得分: 0

原因是它只匹配第一个字符,因为您没有指定一个_定量词_。

因此,一个_字符类_只会匹配一个指定值的字符。

https://[a-z]+(?:\.[a-z]+)+

这会匹配一个 a-z 字符,一个或多个,然后是一个重复模式,一个单独的点字符,一个 a-z 字符,一个或多个。

例如,这将匹配这些值。

https://example.com
https://abc.example.com
https://www.abc.example.com
英文:

The reason it is only matching the first character is because you haven't specified a quantifier.

Thus, a character class will only match one character, of the specified values.

https://[a-z]+(?:\.[a-z]+)+

This is matching an a-z character, one or more, followed by a repeating pattern of, a single dot character, an a-z character, one more.

For example, this will match these values.

https://example.com
https://abc.example.com
https://www.abc.example.com

答案2

得分: -1

尝试将此作为您想要的响应模式,这将很好。

https:\/\/\w+\.com

英文:

try this as your pattern for the response you want this will be good.

https:\\/\\/\\w+\\.com

huangapple
  • 本文由 发表于 2023年6月6日 16:11:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定