简体   繁体   中英

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 ?

好像您的输入包含带重音的单引号。

input.replaceAll("[‘'].*?'", "");
(?<=:).*?(?=[|}])

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM