首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于匹配4-6位ID的URL的RegEx

用于匹配4-6位ID的URL的RegEx
EN

Stack Overflow用户
提问于 2019-05-18 00:06:07
回答 3查看 601关注 0票数 1

我试图匹配以"example.com/"开头的URL,后面跟着4-6位数字,下一个字符不是数字(如果有下一个字符)。

例如,"example.com/12345"应该匹配。

"example.com/1234567"不应该匹配。

"example.com/123456g7"应该匹配。

我尝试过"example.com/(\d{4,6}).*",但是当我给它"example.com/1234567"时,它与它匹配,这是不正确的。

我该如何解决这个问题?

EN

回答 3

Stack Overflow用户

发布于 2019-05-18 02:27:06

该表达式添加额外的边界,只为了安全地传递所需的URL:

代码语言:javascript
复制
^(https?:\/\/(www.)?)(example\.com)\/(?:[0-9]{4,6})?([a-z].*)?$

如果你愿意,你可以减少界限。在这里,我们可以添加几个捕获组来简单地进行调用。

$是你不想要的URL输入失败的关键。

RegEx

如果这不是您想要的表达式,则可以修改/更改regex101.com中的表达式。

RegEx电路

您还可以在jex.im中可视化表达式。

JavaScript演示

代码语言:javascript
复制
const regex = /^(https?:\/\/(www.)?)(example\.com)\/(?:[0-9]{4,6})?([a-z].*)?$/gm;
const str = `http://example.com/12345
https://example.com/123456g7
http://www.example.com/12345
https://www.example.com/123456g7
http://www.example.com/12345
https://www.example.com/123456g7
http://www.example.com/123456adfasdfasdf98989898
https://www.example.com/123456g7adfadfa0909009
http://example.com/1234567
https://example.com/1234567`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Python测试

代码语言:javascript
复制
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"^(https?:\/\/(www.)?)(example\.com)\/(?:[0-9]{4,6})?([a-z].*)?$"

test_str = ("http://example.com/12345\n"
    "https://example.com/123456g7\n"
    "http://www.example.com/12345\n"
    "https://www.example.com/123456g7\n"
    "http://www.example.com/12345\n"
    "https://www.example.com/123456g7\n"
    "http://www.example.com/123456adfasdfasdf98989898\n"
    "https://www.example.com/123456g7adfadfa0909009\n"
    "http://example.com/1234567\n"
    "https://example.com/1234567")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
票数 1
EN

Stack Overflow用户

发布于 2019-05-18 00:07:51

在匹配4至6位数字后,只需对数字进行负前瞻:

代码语言:javascript
复制
example.com\/\d{4,6}(?!\d).*

https://regex101.com/r/YWmhgY/1/

票数 0
EN

Stack Overflow用户

发布于 2019-05-18 01:37:52

另一种方法。

^example\.com/(\d{4,6})(?:\D.*)?$

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56194906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档