tags:

views:

226

answers:

6

Can anybody guide me in the right direction...

I have some folder strucktures that I want to check for a trailing / slash, some of the folders don't have a trailing / and if it does not have a trailing / I need to add one

If I use this regex it works but it replace the last character of the folder name

    Folder/name/test
    folder/name/test1/
    folder/name/test2/
replace(/.$/ig,"/");

This regex replaces Folder/name/tes[t]/ but will take out the t and replace it with /

Hope this makes sense...

A: 

I would avoid a regular expressions in this case and do something easier like:

$path = rtrim($path, '/').'/';

EDIT:

Woops, assumed it was php...

camomileCase
The problem is that it is part of a bigger regex - I use to replace translated textvar strippeden3 = strippeden3.replace(/ü|ù|ú/ig,"u");var strippeden3 = strippeden3.replace(/é|è/ig,"e");var strippeden3 = strippeden3.replace(/æ/ig,"ae");var strippeden3 = strippeden3.replace(/ø|ö|ò|ó/ig,"o");var strippeden3 = strippeden3.replace(/'/ig,"/");var strippeden3 = strippeden3.replace(/å|ä|à|á/ig,"a");var strippeden3 = strippeden3.replace(/,/ig,"");
Gerald Ferreira
+2  A: 
replace(/(.)$/ig,"\1/");

or better

replace(/([^\\])$/ig,"\1/");

if \1 isn't a backreference in your language, then you'll have to figure that out, or tell us teh language.

Tim Hoolihan
I am using Javascript and ASP - replace(/([^\\])$/ig,"\1/"); This solution replaces the last character with a block and then adds the slash after the block - like a unicode character that is missing
Gerald Ferreira
+1  A: 

Without knowing the language it's difficult to post a correct answer and you can't use the code provided in a cut-and-paste fashion. Anyway I might go for this regex:

replace(/(.)\/*$/,"\1/");

This will append the trailing / only if it's not there yet.

Carmine Paolino
Thanks Earcar - I beleive your answer would also have worked
Gerald Ferreira
+6  A: 

Try something like this:

replace(/[^/]$/ig, "$0/")
Gumbo
replace(/[^/]$/ig, "$1/") - Think it would also have worked thanks Gumbo!
Gerald Ferreira
@Gerald Ferreira: `$0` is not a mistake, I used it on purpose.
Gumbo
Why Gumbo ? What is the reason
Gerald Ferreira
`$0` contains the matched string of the whole regular expression.
Gumbo
+1  A: 

The regex you made basically means this: take any character that is the last one in the string and replace it with /. What you need to do is to group the last character and then insert it again in the replacement, like this:

replace(/([^\/])$/ig,"$1/");

For more information see http://www.regular-expressions.info/brackets.html

Schtibe
this would also add back the trailing slash if it exists, resulting in a double slash - equivalent to just appending a slash to everything.
GApple
Thanks a Million Schtibe - just learned something new again :-) It is cool - I thought that I might need to do it in two steps like adding a / to all and then replacing double // with one / again - Your answer is doing both for me in one step!!
Gerald Ferreira
You are right. A better way to do this would bereplace(/([^\/])$/ig,"$1/");This replaces any trailing character that is not a slash
Schtibe
sorry, but this will append another `/` at the end of the line and could be done in a simpler way: `replace(/$/,"/");`.I don't think this is completely correct in that sense
Carmine Paolino
var strippeden3 = strippeden3.replace(/\/\//ig,"/"); <<<< I have this in my code as well which takes a double // and makes it a single slash, so that is why I did not even notice the double slash appearing :-)
Gerald Ferreira
@gerald: looks like an ugly hack..
Carmine Paolino
Edit your solution with the correct solution [^\/] instead of .
Rado
+1  A: 

I'm not sure which language this is for but this is how you would do it in Perl:

#! /local/bin/perl

my @data = <data>;
while (<DATA>)
{
    s#[^/]\n#/\n#m;
    print;
}

__DATA__
/foo/bar/
/baz/jazz
/baz/jazz

This then prints out the following:

/foo/bar/
/baz/jaz/
/baz/jazz/

The key to the regex is the "[^/]\n" This basically matches anything at the end next to to the newline. With your nomenclature, I would assume the syntax would be the following:

replace(/[^\/]\n/ig,"/");

Or if there is no newline use this:

replace(/[^\/]$/ig,"/");

I hope that helps.

Logan