Replace dot in between slash i.e. '/./' with single slash '/'
up vote
4
down vote
favorite
For this string dirPath
,
String dirPath = "c:/create/a/dir/very/deep/inside/../././../../../dir/";
I want the output string to look like :
"c:/create/a/dir/very/deep/inside/../../../../dir/";
I used :
dirPath.replaceAll("/[.]/", "/");
but that gave :
c:/create/a/dir/very/deep/inside/.././../../../dir/
^^^
then, tried with one more replaceAll
as:
dirPath.replaceAll("/[.]/", "/").replaceAll("/[.]/", "/");
and that worked!
My question is why couldn't one call achieve the same result?
How to achieve it in simplest way?
P.S. Another regex that didn't work for me : .replaceAll("($|/)[.]/", "$1")
java regex replace
|
show 1 more comment
up vote
4
down vote
favorite
For this string dirPath
,
String dirPath = "c:/create/a/dir/very/deep/inside/../././../../../dir/";
I want the output string to look like :
"c:/create/a/dir/very/deep/inside/../../../../dir/";
I used :
dirPath.replaceAll("/[.]/", "/");
but that gave :
c:/create/a/dir/very/deep/inside/.././../../../dir/
^^^
then, tried with one more replaceAll
as:
dirPath.replaceAll("/[.]/", "/").replaceAll("/[.]/", "/");
and that worked!
My question is why couldn't one call achieve the same result?
How to achieve it in simplest way?
P.S. Another regex that didn't work for me : .replaceAll("($|/)[.]/", "$1")
java regex replace
I don't know if there is a one liner way to do, but one solution might to loop while theString
contains/./
and continue to replace its occurrence :/
– MadProgrammer
Nov 9 at 4:32
IMO, using loop will be overkill for this simple yet general task.
– Saurav Sahu
Nov 9 at 4:56
Never said it was a good idea, just didn't have a better solution ;)
– MadProgrammer
Nov 9 at 5:01
Why do you need to remove the dots, but not normalize for the dot dots? Surely if whatever you are passing this string to can handle both or neither?
– Andy Turner
Nov 9 at 7:45
I am passing this converted string to Java glob path matcher, which matches it against literal dots. That's why conversion is absolutely necessary. Read more here docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob
– Saurav Sahu
Nov 9 at 8:44
|
show 1 more comment
up vote
4
down vote
favorite
up vote
4
down vote
favorite
For this string dirPath
,
String dirPath = "c:/create/a/dir/very/deep/inside/../././../../../dir/";
I want the output string to look like :
"c:/create/a/dir/very/deep/inside/../../../../dir/";
I used :
dirPath.replaceAll("/[.]/", "/");
but that gave :
c:/create/a/dir/very/deep/inside/.././../../../dir/
^^^
then, tried with one more replaceAll
as:
dirPath.replaceAll("/[.]/", "/").replaceAll("/[.]/", "/");
and that worked!
My question is why couldn't one call achieve the same result?
How to achieve it in simplest way?
P.S. Another regex that didn't work for me : .replaceAll("($|/)[.]/", "$1")
java regex replace
For this string dirPath
,
String dirPath = "c:/create/a/dir/very/deep/inside/../././../../../dir/";
I want the output string to look like :
"c:/create/a/dir/very/deep/inside/../../../../dir/";
I used :
dirPath.replaceAll("/[.]/", "/");
but that gave :
c:/create/a/dir/very/deep/inside/.././../../../dir/
^^^
then, tried with one more replaceAll
as:
dirPath.replaceAll("/[.]/", "/").replaceAll("/[.]/", "/");
and that worked!
My question is why couldn't one call achieve the same result?
How to achieve it in simplest way?
P.S. Another regex that didn't work for me : .replaceAll("($|/)[.]/", "$1")
java regex replace
java regex replace
asked Nov 9 at 4:26
Saurav Sahu
6,31332544
6,31332544
I don't know if there is a one liner way to do, but one solution might to loop while theString
contains/./
and continue to replace its occurrence :/
– MadProgrammer
Nov 9 at 4:32
IMO, using loop will be overkill for this simple yet general task.
– Saurav Sahu
Nov 9 at 4:56
Never said it was a good idea, just didn't have a better solution ;)
– MadProgrammer
Nov 9 at 5:01
Why do you need to remove the dots, but not normalize for the dot dots? Surely if whatever you are passing this string to can handle both or neither?
– Andy Turner
Nov 9 at 7:45
I am passing this converted string to Java glob path matcher, which matches it against literal dots. That's why conversion is absolutely necessary. Read more here docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob
– Saurav Sahu
Nov 9 at 8:44
|
show 1 more comment
I don't know if there is a one liner way to do, but one solution might to loop while theString
contains/./
and continue to replace its occurrence :/
– MadProgrammer
Nov 9 at 4:32
IMO, using loop will be overkill for this simple yet general task.
– Saurav Sahu
Nov 9 at 4:56
Never said it was a good idea, just didn't have a better solution ;)
– MadProgrammer
Nov 9 at 5:01
Why do you need to remove the dots, but not normalize for the dot dots? Surely if whatever you are passing this string to can handle both or neither?
– Andy Turner
Nov 9 at 7:45
I am passing this converted string to Java glob path matcher, which matches it against literal dots. That's why conversion is absolutely necessary. Read more here docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob
– Saurav Sahu
Nov 9 at 8:44
I don't know if there is a one liner way to do, but one solution might to loop while the
String
contains /./
and continue to replace its occurrence :/– MadProgrammer
Nov 9 at 4:32
I don't know if there is a one liner way to do, but one solution might to loop while the
String
contains /./
and continue to replace its occurrence :/– MadProgrammer
Nov 9 at 4:32
IMO, using loop will be overkill for this simple yet general task.
– Saurav Sahu
Nov 9 at 4:56
IMO, using loop will be overkill for this simple yet general task.
– Saurav Sahu
Nov 9 at 4:56
Never said it was a good idea, just didn't have a better solution ;)
– MadProgrammer
Nov 9 at 5:01
Never said it was a good idea, just didn't have a better solution ;)
– MadProgrammer
Nov 9 at 5:01
Why do you need to remove the dots, but not normalize for the dot dots? Surely if whatever you are passing this string to can handle both or neither?
– Andy Turner
Nov 9 at 7:45
Why do you need to remove the dots, but not normalize for the dot dots? Surely if whatever you are passing this string to can handle both or neither?
– Andy Turner
Nov 9 at 7:45
I am passing this converted string to Java glob path matcher, which matches it against literal dots. That's why conversion is absolutely necessary. Read more here docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob
– Saurav Sahu
Nov 9 at 8:44
I am passing this converted string to Java glob path matcher, which matches it against literal dots. That's why conversion is absolutely necessary. Read more here docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob
– Saurav Sahu
Nov 9 at 8:44
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
You can use a lookahead pattern to avoid consuming the slash needed by the subsequent match:
dirPath.replaceAll("/\.(?=/)", "")
Demo: https://regex101.com/r/qWKVU3/1 or http://tpcg.io/ijmYJF
Works really well. Thanks. Any good reference to learn about this look-ahead pattern?
– Saurav Sahu
Nov 9 at 4:52
Should we prefer\.
over[.]
to match a single dot? Some recommends the later one i.e.[.]
with brackets.
– Saurav Sahu
Nov 9 at 4:54
Glad to be of help. You can learn more about lookahead and lookbehind patterns here. I personally prefer escaping dots with backslashes because square brackets are really meant to include two or more characters.
– blhsing
Nov 9 at 5:02
Thus wouldn't strip/.
at the end of the string. Don't know if that is important to OP.
– Andy Turner
Nov 9 at 7:44
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
You can use a lookahead pattern to avoid consuming the slash needed by the subsequent match:
dirPath.replaceAll("/\.(?=/)", "")
Demo: https://regex101.com/r/qWKVU3/1 or http://tpcg.io/ijmYJF
Works really well. Thanks. Any good reference to learn about this look-ahead pattern?
– Saurav Sahu
Nov 9 at 4:52
Should we prefer\.
over[.]
to match a single dot? Some recommends the later one i.e.[.]
with brackets.
– Saurav Sahu
Nov 9 at 4:54
Glad to be of help. You can learn more about lookahead and lookbehind patterns here. I personally prefer escaping dots with backslashes because square brackets are really meant to include two or more characters.
– blhsing
Nov 9 at 5:02
Thus wouldn't strip/.
at the end of the string. Don't know if that is important to OP.
– Andy Turner
Nov 9 at 7:44
add a comment |
up vote
5
down vote
accepted
You can use a lookahead pattern to avoid consuming the slash needed by the subsequent match:
dirPath.replaceAll("/\.(?=/)", "")
Demo: https://regex101.com/r/qWKVU3/1 or http://tpcg.io/ijmYJF
Works really well. Thanks. Any good reference to learn about this look-ahead pattern?
– Saurav Sahu
Nov 9 at 4:52
Should we prefer\.
over[.]
to match a single dot? Some recommends the later one i.e.[.]
with brackets.
– Saurav Sahu
Nov 9 at 4:54
Glad to be of help. You can learn more about lookahead and lookbehind patterns here. I personally prefer escaping dots with backslashes because square brackets are really meant to include two or more characters.
– blhsing
Nov 9 at 5:02
Thus wouldn't strip/.
at the end of the string. Don't know if that is important to OP.
– Andy Turner
Nov 9 at 7:44
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
You can use a lookahead pattern to avoid consuming the slash needed by the subsequent match:
dirPath.replaceAll("/\.(?=/)", "")
Demo: https://regex101.com/r/qWKVU3/1 or http://tpcg.io/ijmYJF
You can use a lookahead pattern to avoid consuming the slash needed by the subsequent match:
dirPath.replaceAll("/\.(?=/)", "")
Demo: https://regex101.com/r/qWKVU3/1 or http://tpcg.io/ijmYJF
answered Nov 9 at 4:32
blhsing
27.6k41335
27.6k41335
Works really well. Thanks. Any good reference to learn about this look-ahead pattern?
– Saurav Sahu
Nov 9 at 4:52
Should we prefer\.
over[.]
to match a single dot? Some recommends the later one i.e.[.]
with brackets.
– Saurav Sahu
Nov 9 at 4:54
Glad to be of help. You can learn more about lookahead and lookbehind patterns here. I personally prefer escaping dots with backslashes because square brackets are really meant to include two or more characters.
– blhsing
Nov 9 at 5:02
Thus wouldn't strip/.
at the end of the string. Don't know if that is important to OP.
– Andy Turner
Nov 9 at 7:44
add a comment |
Works really well. Thanks. Any good reference to learn about this look-ahead pattern?
– Saurav Sahu
Nov 9 at 4:52
Should we prefer\.
over[.]
to match a single dot? Some recommends the later one i.e.[.]
with brackets.
– Saurav Sahu
Nov 9 at 4:54
Glad to be of help. You can learn more about lookahead and lookbehind patterns here. I personally prefer escaping dots with backslashes because square brackets are really meant to include two or more characters.
– blhsing
Nov 9 at 5:02
Thus wouldn't strip/.
at the end of the string. Don't know if that is important to OP.
– Andy Turner
Nov 9 at 7:44
Works really well. Thanks. Any good reference to learn about this look-ahead pattern?
– Saurav Sahu
Nov 9 at 4:52
Works really well. Thanks. Any good reference to learn about this look-ahead pattern?
– Saurav Sahu
Nov 9 at 4:52
Should we prefer
\.
over [.]
to match a single dot? Some recommends the later one i.e. [.]
with brackets.– Saurav Sahu
Nov 9 at 4:54
Should we prefer
\.
over [.]
to match a single dot? Some recommends the later one i.e. [.]
with brackets.– Saurav Sahu
Nov 9 at 4:54
Glad to be of help. You can learn more about lookahead and lookbehind patterns here. I personally prefer escaping dots with backslashes because square brackets are really meant to include two or more characters.
– blhsing
Nov 9 at 5:02
Glad to be of help. You can learn more about lookahead and lookbehind patterns here. I personally prefer escaping dots with backslashes because square brackets are really meant to include two or more characters.
– blhsing
Nov 9 at 5:02
Thus wouldn't strip
/.
at the end of the string. Don't know if that is important to OP.– Andy Turner
Nov 9 at 7:44
Thus wouldn't strip
/.
at the end of the string. Don't know if that is important to OP.– Andy Turner
Nov 9 at 7:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219875%2freplace-dot-in-between-slash-i-e-with-single-slash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I don't know if there is a one liner way to do, but one solution might to loop while the
String
contains/./
and continue to replace its occurrence :/– MadProgrammer
Nov 9 at 4:32
IMO, using loop will be overkill for this simple yet general task.
– Saurav Sahu
Nov 9 at 4:56
Never said it was a good idea, just didn't have a better solution ;)
– MadProgrammer
Nov 9 at 5:01
Why do you need to remove the dots, but not normalize for the dot dots? Surely if whatever you are passing this string to can handle both or neither?
– Andy Turner
Nov 9 at 7:45
I am passing this converted string to Java glob path matcher, which matches it against literal dots. That's why conversion is absolutely necessary. Read more here docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob
– Saurav Sahu
Nov 9 at 8:44