Golang中的正则表达式斜线

我有两个链接:</ p>

  1:/ aaa /  bbbb / ccccc.htm 
2:/xxx/yyy.htm
</ pre>

哪些正则表达式能够匹配第二个链接?</ p>

< p>我尝试过:</ p>

  ^ \ /.* \ /.* [^ \ /]。* 
</ code> </ pre>

但是,它与所有匹配。</ p>
</ div>

展开原文

原文

I have two links:

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

What regex is able to match the second link?

I have tried:

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

But, it match all of them.

douhang5493
douhang5493 ^(?:\/\w+){2}\。htm这应该是您想要的!
大约一年之前 回复
dotaer1993
dotaer1993 ^/[^/]+/[^/]+$,以/开头,然后是非/字符串,以/开头,以非/字符串结尾
大约一年之前 回复

1个回答

I'm guessing that we might want to pass both URLs, which in that case we would start with:

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

We can then add more boundaries, if you wish.

RegEx

If this wasn't your desired expression, you can modify/change your expressions in regex101.com.

enter image description here

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

JavaScript Group Breakup

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

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

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


If you only wish to pass the second URL and fail the first one, you can simply add some boundaries to your expression, maybe something similar to this would work:

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

enter image description here

</div>
douza19870617
douza19870617 非常感谢,您救了我的一天.....
大约一年之前 回复
dtcmadj31951
dtcmadj31951 对于Go问题,请不要忘记将regex101更改为使用RE2引擎而不是PCRE。 我自由地更新了链接。
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问