views:

100

answers:

2

Hi,

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

+1  A: 

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.

Joey
Sorry for not being clear. I wanted to match it using regex. I have 100s of normal links that I'm trying to convert to regex in bulk. Using some pseudo methods I was able to format most of the links except for the slashes after the domain.Thanks for all the help.
Perhaps it's a limitation of Notepad++ or I am simply forgetting how to setup the command, but n++ is not letting me search the document using your answer. Certainly looks like what I need though.
@user: Ah, SciTE, the underlying editor engine of Notepad++ has pretty crappy regex support. It's not very nice but somehow they never saw the need of fixing it. I'm adding something that might work; just a moment.
Joey
Better put the look-behind after the `/`: `/(?<=[a-zA-Z0-9]/)(?=[a-zA-Z0-9])`
Gumbo
@Gumbo: Just out of curiosity: What does that accomplish? Just that the RE engine doesn't try looking for a `/` after every alphanumeric char? (I don't know enough about how they work internally, hence the question.) Anyway, it won't work with lookaround anyway since Notepad++ is *coughs* a little braindead regarding regexes.
Joey
@Johannes Rössel: When putting the look-behind in front of the `/`, the look-behind would be applied for each character; after the `/` it would only be tested if a `/` was already found.
Gumbo
I figured it might be an issue with Notepad++, hehe. Is there an alternative editor you would recommend that is a bit more current? I found N++ to be extremely powerful but it did feel a bit outdated at times.
@user: Not that I know of. I rarely need regular expressions in my editor anyway. Far introduced support for it sometime in Far 2, but I haven't tried it yet (and I don't know what features are there or what RE flavor it is).
Joey
Daniel
A: 

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

Funny (duh).Yes, that's what I was trying to accomplish, but without having to do it manually one by one for 100s of links.