0

In java I tried to replace .JPG.json part with .JPG, saving letter case ( sometimes it maybe .jpg.json)

My Code :

String Path="MyImage.JPG.json";
Result=Path.replaceFirst("/(.jpg)\\.json/i","$1");

But it returns : MyImage.JPG.json

Instead of: MyImage.JPG

2 Answers 2

1

You need to remove the / slashes. In java, you don't need to include / as a regex delimiter. And also you must need to escape the dots. To do a case-insensitive match, add (?i) modifier at the first.

Path.replaceFirst("(?i)(\\.jpg)\\.json", "$1");

OR

You could use lookbehind assertion also.

Path.replaceFirst("(?i)(?<=\\.jpg)\\.json", "");

(?<=\\.jpg) Asserts that the string going to be matched must be preceded by .jpg. If yes then match only the following .json string. Replacing the matched .json string with an empty string will give you the desired output.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this command. It should work for you:

Path.replaceFirst("(?i)(\\.jpg)\\.json","$1")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.