1

如果它是一个数字,我试图忽略斜线后的所有内容 -

http://www.example.com/123abc/456/ABC/789/

所需的输出是

http://www.example.com/123abc/

到目前为止,我已经尝试了以下方法 -

(https?:\/\/.*)(?=/\d+).*

这给了我 -

http://www.example.com/123abc/456/ABC/

非常感谢!

4

2 回答 2

2
于 2013-04-17T17:48:24.880 回答
1

The .* is greedy and will try to match as much as possible. The 789 existence allows for a match of everything up to it. Instead you can use.

(https?:\/\/.*?)(?=/\d+).*

The ? makes the .* relucant, so it will match as little as possible to satisfy the expression.

However, this doesn't fulfill the requirement you described which is actually "Ignore everything after the second slash if it is a number." You can use (in your specific case):

(https?:\/\/.*?\/.*?\/)(?=\d+).*
于 2013-04-17T17:48:50.980 回答