Bankelaal
Bankelaal

Reputation: 418

How to remove string between two characters using regex

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 ?

Upvotes: 0

Views: 307

Answers (3)

vks
vks

Reputation: 67968

(?<=:).*?(?=[|}])

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 }

Upvotes: 0

joey rohan
joey rohan

Reputation: 3566

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

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

Seems like your input contain accented single quote.

input.replaceAll("[‘'].*?'", "");

Upvotes: 3

Related Questions