简单的正则表达式 url 相关匹配帮助

发布于 2024-08-21 15:01:31 字数 230 浏览 5 评论 0原文

采用以下 URI:

http:\/\/.*\.google\.com/search/results\.php*

我只是尝试匹配给定 URI 中两个字母数字字符之间的所有单正斜杠 ( / )。在上面的例子中,两个就在搜索和结果之前。你能指出我正确的方向吗?

编辑

目的是使用 Notepad++ 进行搜索和替换。

Take the following URI:

http:\/\/.*\.google\.com/search/results\.php*

I am simply trying to match all single forward slashes ( / ) between two alphanumeric characters in a given URI. In the case above, the two just before search and results. Could you point me in the right direction?

EDIT

The intention is to do search and replace using Notepad++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九歌凝 2024-08-28 15:01:31

http://..google.com/search/results.php

http://..google.com/search/results.php

儭儭莪哋寶赑 2024-08-28 15:01:31

不太确定你在那里做什么,因为你的“URI”似乎已经是一个正则表达式。

但要匹配嵌入在字母数字字符之间的斜杠 (/),您可以使用:

/(?<=[a-zA-Z0-9]/)(?=[a-zA-Z0-9])

正向后查找和先行查找确保斜杠确实位于两个字母数字字符之间。

在 Windows PowerShell 中测试:

PS Home:\> $uri='http://stackoverflow.com/questions/2259778/simple-regex-url-related-matching-help'
PS Home:\> [regex]::matches($uri, '(?<=[a-zA-Z0-9])/(?=[a-zA-Z0-9])') | ft -auto

Groups   Success   Captures   Index   Length   Value
------   -------   --------   -----   ------   -----
{/}         True   {/}           24        1   /
{/}         True   {/}           34        1   /
{/}         True   {/}           42        1   /

ETA: 如果我理解正确的话,您想用 \/ 替换嵌入在两个字母数字字符之间的斜杠来转义它们,对吧?

然后将以下内容替换

([a-zA-Z-0-9])/([a-zA-Z-0-9])

\1\\/\2

应该可以。这不仅仅捕获斜杠(如上面的方法;由于 Notepad++ 的限制),因此我们还必须重新插入周围的字符。

但是,您可能无论如何都想搜索未转义的斜杠。因此,我认为替换

([^\\])/

\1\\/

更有意义。这将搜索前面没有反斜杠的斜杠。

Not really sure what you're doing there, as your “URI” is seemingly already a regex.

But to match a slash (/) embedded between alphanumeric characters, you can use:

/(?<=[a-zA-Z0-9]/)(?=[a-zA-Z0-9])

A positive lookbehind and lookahead make sure that the slash is indeed between two alphanumeric characters.

Test in Windows PowerShell:

PS Home:\> $uri='http://stackoverflow.com/questions/2259778/simple-regex-url-related-matching-help'
PS Home:\> [regex]::matches($uri, '(?<=[a-zA-Z0-9])/(?=[a-zA-Z0-9])') | ft -auto

Groups   Success   Captures   Index   Length   Value
------   -------   --------   -----   ------   -----
{/}         True   {/}           24        1   /
{/}         True   {/}           34        1   /
{/}         True   {/}           42        1   /

ETA: If I understood you correctly you want to replace slashes embedded between two alphanumeric characters by \/ to escape them, right?

Then replacing the following

([a-zA-Z-0-9])/([a-zA-Z-0-9])

by

\1\\/\2

should work. This doesn't capture the slash only (as above method; due to limitations of Notepad++) therefore we have to reinsert the surrounding characters as well.

However, you probably want to search for unescaped slashes anyway. So replacing

([^\\])/

by

\1\\/

would make more sense, I think. This searches for a slash that is not preceded by a backslash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文