Regex 返回前8个字符的正则表达式匹配
我正在尝试匹配域的前8个字符。目前我的规则是: 必须只有前8个 如果不存在8个字符,则抓取Regex 返回前8个字符的正则表达式匹配,regex,Regex,我正在尝试匹配域的前8个字符。目前我的规则是: 必须只有前8个 如果不存在8个字符,则抓取.tld 我的正则表达式目前看起来是这样的:^(www\)?(?。+)$ 这就像谷歌网站google.com 在我的情况下,我会让它抓取google 在thisislongerthe8characterslong.com的情况下,我想返回thisislo您需要在上使用量词: ^(?:www\.)?(?<domain>.{0,8}).*\..{2,3}$ ^(?:www\)?(?。{0,8})。
.tld
我的正则表达式目前看起来是这样的:^(www\)?(?。+)$
这就像谷歌网站google.com
在我的情况下,我会让它抓取google
在
thisislongerthe8characterslong.com
的情况下,我想返回thisislo
您需要在上使用量词:
^(?:www\.)?(?<domain>.{0,8}).*\..{2,3}$
^(?:www\)?(?。{0,8})。*\..{2,3}$
范例
匹配项:
google.com : google
thisisanenormousdomain.co.uk : thisisan
google.co.uk : google.c
www.google.com : google
www.thisisanenormousdomain.co.uk : thisisan
技术细节:
^(?:www\.)?(.{1,8}).*\.[A-Za-z0-9]+$
│└────┬───┘└───┬──┘└┬─┘└─────┬─────┘
│ │ │ │ └ 5. Match TLD (e.g. com, uk, org, net, etc)
│ │ │ └ 4. Match anything after the first eight characters, up until the last '.' in the url.
│ │ └ 3. Capture between one and eight characters inside a match group.
│ └ 2. Match an optional www. prefix. Do not add to match groups.
└ 1. Find the start of the string.
试试看:如果使用images.google.com
,会发生什么情况-我应该使用google
还是images.g
?@josh.trow这是正确的,它将返回images.g
您在哪里和什么环境下使用这个正则表达式?只匹配一个域,然后在其上使用子字符串方法不是更容易吗?(BTW:<代码> /^(WWW))({{1,8}(?=*[^?] +$))/ GM < /代码>很大程度上取决于您是否考虑<代码>英.CO.UK/<代码>具有“代码< >的TLD。U/<代码>或<代码> .CO.UK/<代码> BTW.AUIIO -没问题!如果您有帮助的话,我已经添加了正则表达式的技术故障:)啊,该死,应该在评论之前重新加载:(+1我来到这里是为了寻找完全不同的东西,但+1是为了更好的解释!以\..{2,3}
结尾是古老的历史(即使在那时,像.museum
这样罕见的tld已经存在)。但是从现在开始,不要假设tld的长度有限制;)ICANN扔掉新的和长的tld就像什么都没有一样。
^(?:www\.)?(.{1,8}).*\.[A-Za-z0-9]+$
│└────┬───┘└───┬──┘└┬─┘└─────┬─────┘
│ │ │ │ └ 5. Match TLD (e.g. com, uk, org, net, etc)
│ │ │ └ 4. Match anything after the first eight characters, up until the last '.' in the url.
│ │ └ 3. Capture between one and eight characters inside a match group.
│ └ 2. Match an optional www. prefix. Do not add to match groups.
└ 1. Find the start of the string.