Golang 正则表达式中的斜线

Slash in regular expression in Golang

我有两个 link:

1: /aaa/bbbb/ccccc.htm
2: /xxx/yyy.htm

哪个正则表达式能够匹配第二个 link?

我试过:

^\/.*\/.*[^\/].* 

但是,它匹配所有这些。

我猜我们可能想同时传递两个 URLs,在这种情况下我们将从:

开始
(\/[a-z]+)?(?:\.htm)?

如果您愿意,我们可以添加更多边界。

正则表达式

如果这不是您想要的表达方式,您可以 modify/change 您的表达方式 regex101.com

正则表达式电路

jex.im 可视化正则表达式:

JavaScript 小组解散

const regex = /((\/[a-z]+)?(?:\.htm)?)/gm;
const str = `/aaa/bbbb/ccccc.htm
/xxx/yyy.htm`;
const subst = `Group #1: \nGroup #2: \n`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);


如果你只想通过第二个 URL 而第一个不及格,你可以简单地在你的表达式中添加一些边界,也许类似于 this 的东西会起作用:

^\/[a-z]+\/[a-z]+.htm$