0

Well I have this code:

String replacedItemName = ItemDefinitions.getItemDefinitions(usedWith).getName().replaceAll("\\(.\\)", "(6)");

Is \\(.\\) the correct regex to replace anyting in the brackets of the item name? (Java)

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jonathan Beaudoin
  • 2,158
  • 4
  • 27
  • 63
  • 4
    Instead of changing your question to something entirely different, and thus having the answers make no sense at all, you should accept the correct answer and start a new question. – WouterH Aug 19 '12 at 21:04
  • possible duplicate of [replace string in parentheses using regex](http://stackoverflow.com/questions/1138552/replace-string-in-parentheses-using-regex) – tripleee Aug 20 '12 at 04:07

2 Answers2

1

I would suggest to use replaceAll("(?<=\\().*?(?=\\))", "6");. See here

Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
Ωmega
  • 42,614
  • 34
  • 134
  • 203
-1

Almost, you forgot a plus (one-or-more) after your dot. Without the plus, the dot only matches one character.

\(.+\)

However, I am unsure what strings you are targeting. I've made a Rubular with some examples:

http://rubular.com/r/0WijBsdtV0

Do these match your intended behavior?

Martin Devillers
  • 17,293
  • 5
  • 46
  • 88
  • @Uhehesh: No need to escape the `)` inside `[]`s. – Keppil Aug 19 '12 at 21:01
  • Don't use `\(.+\)` - it is greedy and can match more then you want! >> http://rubular.com/r/ANfAVC1nJd – Ωmega Aug 19 '12 at 21:03
  • @Ωmega I know, I even included this limitation in one of my examples in the Rubular snippet. I am not sure what his scenario is, so I do not know if his input string will contain a single block of parentheses or multiple blocks. By showing him a Rubular with some examples I hope he can figure out his own solution. – Martin Devillers Aug 19 '12 at 21:05