Archit Arora
Archit Arora

Reputation: 2636

Delete the content of a string between a word and special character in java

I have a string like this -

['name' {d763e18f-1719-480b-bcd6-8fea7bad894e} Parameter, 'class' {8471633e-4a54-4c86-bd2b-56d58baf2fbb} Parameter, 'id' {23471633e-4a54-4c86-bd2b-56d58baf2fbb} Parameter]

And I want the following result -

['name' , 'class' , 'id']

All the content between the word in quotes '' and , should be deleted.

How do I achieve this? Thanks!

Upvotes: 5

Views: 127

Answers (3)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136162

try this

Matcher m = Pattern.compile("'.+?'").matcher(str);
StringBuilder sb = new StringBuilder();
while(m.find()) {
    if (sb.length() > 0) {
        sb.append(',');
    }
    sb.append(m.group());
}
String res = sb.toString();

Upvotes: 0

Ranjeet
Ranjeet

Reputation: 634

You can use this regex.

('\w+'\s*).*?(?=[,\]])

Upvotes: 1

karthik manchala
karthik manchala

Reputation: 13650

You can use the following to match:

('\w+'\s*).*?(?=[,\]])

And replace with $1

regex in java would be ('\\w+'\\s*).*?(?=[,\\]])

See DEMO

Upvotes: 2

Related Questions