英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论