0

I have a string like below

{A:'XYZ'|B:‘123'}.[{C:‘pqr'}.{p:'a'}].{I1:'t123'|I2:'345'}

I want to remove all the characters between ' and ' and want a final result like

{A:|B:}.[{C:}.{p:}].{I1:|I2:}

I am using the regx like below

input.replaceAll("'.*?'", "");

But unable to get the desired result. Can someone point out what am I missing here ?

3 Answers 3

3

Seems like your input contain accented single quote.

input.replaceAll("[‘'].*?'", "");
Sign up to request clarification or add additional context in comments.

Comments

0

What you are doing : '.*?'

Let us see what is wrong. the symbol '.' should be used very carefully and only when needed. '.' in your case also consumes the chars which you don't what.

According to the patten, at first we can have a or '. So we can have (‘|') .Next we a looking for alphabets or numbers: [a-zA-Z1-9]* . In the end, same closing with ' . We have:

('|‘)[a-zA-Z1-9]+'

https://regex101.com/r/uK9cD8/4

2 Comments

This looks wrong on multiple fronts. Your pattern starts by looking for '*‘*, which doesn't have to match any characters at all. There's nothing in the problem that says the characters between quotes have to be letters or numbers. And I have no idea why the + at the end is there. If the rest of the pattern were correct, this would cause it to match consecutive sequences of that pattern--but it doesn't matter, since replaceAll would remove them all anyway, with or without the +.
Better. But your statement that "'.' in your case also consumes the chars which you don't what", whatever that means, is questionable.
0
(?<=:).*?(?=[|}])

You can use lookarounds here.This way you dont need to worry about all the types of quotes and their combinations.See demo.

https://regex101.com/r/uK9cD8/2

This basically removes everything from : to first | or }.This way you achieve what you want without caring about the contents between : and | or }

1 Comment

It would help a lot if you explain what you are doing, since your regex looks very cryptic and since you are answering the question you think the OP should have asked instead of the question the OP asked (which was about removing the stuff in quotes), which means that readers who can't read your mind won't know what the heck you're trying to do.

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.