开发者

Simple regex url-related matching help

开发者 https://www.devze.com 2022-12-20 15:41 出处:网络
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 ca

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++.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消