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")










share|improve this question





















  • 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














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")










share|improve this question





















  • 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












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")










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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















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












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






share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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






share|improve this answer




















  • 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














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






share|improve this answer




















  • 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












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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)