0

I would like to replace all occurences of strings like:

"{something1}
"{someother2}
"{thing3}

but how to deal with group that contains string, not chars?

-- edit:

e.g. given String:

sometext "{something1}hello

I would like to have

sometext hello

or better, but its only replaceAll parameter

sometext "hello
Mateusz Chromiński
  • 2,742
  • 4
  • 28
  • 45
  • 3
    What do you mean by "string, not chars"? –  Aug 25 '11 at 10:32
  • i know how to use patterns like this: String pattern="[abc]" - this group contains chars. But I would like to make group of strings, not chars – Mateusz Chromiński Aug 25 '11 at 10:37
  • I don't quite understand what you want to achive by this. What kind of Strings do you want to match? –  Aug 25 '11 at 10:38

3 Answers3

3

I guess you can use replaceAll:

String b = a.replaceAll("\\{.*?\\}", "sometext ");

This will replace all characters surrounded by curly braces with the replacement string.

JK.
  • 5,126
  • 1
  • 27
  • 26
1

Just build a regular expression using the | operator inside a group.

  • nor this: String pattern = "\"(\\{String\\}|\\{Long\\}|\\{Boolean\\}|\\{Number\\})"; nor this: String pattern = "\"({String}|{Long}|{Boolean}|{Number})"; have worked – Mateusz Chromiński Aug 25 '11 at 10:44
1

You could use the or '|' operator to match full strings -

subject.replace(/something1|someother2|thing3/g, ","); 
ipr101
  • 24,096
  • 8
  • 59
  • 61