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
3
  • 3
    What do you mean by "string, not chars"? Commented Aug 25, 2011 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 Commented Aug 25, 2011 at 10:37
  • I don't quite understand what you want to achive by this. What kind of Strings do you want to match? Commented Aug 25, 2011 at 10:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for advice. I have completely forgot that String are immutable.
1

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

1 Comment

nor this: String pattern = "\"(\\{String\\}|\\{Long\\}|\\{Boolean\\}|\\{Number\\})"; nor this: String pattern = "\"({String}|{Long}|{Boolean}|{Number})"; have worked
1

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

subject.replace(/something1|someother2|thing3/g, ","); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.