Dmitry Pekach
Dmitry Pekach

Reputation: 161

How to replace all squared brackets and commas using regex in java?

Having next line:

String filtered = data.replaceAll("//[ /,]"," ");

where data is like to ["blahblah","blahblah"], but nothing good happens

How to remove all unnecessary symbols and get filtered like "blablah blahblah"?

Upvotes: 2

Views: 1859

Answers (2)

abioticGirl
abioticGirl

Reputation: 188

If you want to have a space between words you need to replace , to " ":

String filtered = data.replaceAll("\\[|\\]|\"","");
filtered = filtered.replaceAll(","," ");

Now the output is "blahblah blahblah"

Upvotes: 3

Shafizadeh
Shafizadeh

Reputation: 10340

Use this pattern:

"\\W+"

Online Demo

  • " delimiter
  • \ escapes \
  • \W+ match one or more non-word character(s)

Full code:

String filtered = data.replaceAll("\\W+","");

Upvotes: 7

Related Questions