Question : regular expression - replace underscore with hyphen

Hi Experts,

I've got Helicon's Isapi Rewrite Manager v3 sitting on a Windows server and I'm trying to write some regular expression to help me with 300-400 URL rewrites.

Here is an example of what I have and what I want...

folder1/folder2/word_word_word.htm  ---> http://www.newdomain.co.uk/folder1/folder2/word-word-word.htm

I might also find just two words separated with one underscore or even four words with three underscores but I'm guessing I can write multiple rules for this.

Here is an example of what I have already which doesn't to work...

RewriteRule ^(/folder1/folder2/[^_/]+)_([^_/])\.htm$ http://www.newdomain.co.uk/folder1/folder2/$1-$2.htm [NC,R=301,L]
RewriteRule ^(/folder1/folder2/[^_/]+)_([^_/])_([^_/])\.htm$ http://www.newdomain.co.uk/folder1/folder2/$1-$2-$3.htm [NC,R=301,L]

Can any one show me where I'm going wrong?

I've also tried...

RewriteRule ^(/folder1/folder2/[^_/]+)_([^_/])\.htm$ http://www.newdomain.co.uk/$1-$2.htm [NC,R=301,L]

but again with no luck.


Many thanks!

Answer : regular expression - replace underscore with hyphen

>>  RewriteRule ^(/folder1/folder2/[^_/]+)_([^_/])\.htm$ http://www.newdomain.co.uk/folder1/folder2/$1-$2.htm [NC,R=301,L]
>>  RewriteRule ^(/folder1/folder2/[^_/]+)_([^_/])_([^_/])\.htm$ http://www.newdomain.co.uk/folder1/folder2/$1-$2-$3.htm

For each subsequent "[^_/]" after the first, you are only checking for one character before going to the next underscore. In other words, you are missing your quantifiers:
1:
2:
RewriteRule ^(/folder1/folder2/[^_/]+)_([^_/]+)\.htm$ http://www.newdomain.co.uk/folder1/folder2/$1-$2.htm [NC,R=301,L]
RewriteRule ^(/folder1/folder2/[^_/]+)_([^_/]+)_([^_/]+)\.htm$ http://www.newdomain.co.uk/folder1/folder2/$1-$2-$3.htm
Random Solutions  
 
programming4us programming4us