我正在尝试匹配域的前8个字符。目前我的规则是:
必须只是第8个
如果不存在8个字符,请抓住.tld
之前的每个字符
我的正则表达式目前如此:^(www\.)?(?<domain>.+)$
就像google.com
在我的情况下,我会抓住google
在thisislongerthen8characterslong.com
的情况下,我想返回thisislo
答案 0 :(得分:4)
^(?:www\.)?(.{1,8}).*\.[A-Za-z0-9]+$
匹配
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.
答案 1 :(得分:0)