• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to replace the bracket in java?

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi good evening,


i am trying to change the brackets for the following inputs such as any thing given within "[ ]" i want that to be replaced by "( )?" the bracket change followed by question mark the other things should be the same


apple[s]
apple|Mango[es]
Mango[s]
Papaya[s] | apple | mango
Papaya | apple


Expected output

apple(s)?
apple|Mango(es)?
Mango(s)?
Papaya(s)? | apple | mango
Papaya | apple


i have taken this in a file and now i have that string s



here i am getting only the same output.
there is no change in the input and output. can any body help me ?
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank's for your reply i am able to get the correct output

but Aniruddh Joshi sorry to tell that there is no change in this step you have given. the input and the output is the same.
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry to tell that there is no change in this step you have given. the input and the output is the same.


You should use


if you want to see the changes.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you don't need the power of a regular expression, I would recommend using replace(...) -- not replaceAll(...).
edit: And in case it's a concern, replace(...) also replaces all occurrences.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi yaar,

the code does not work for this statement even after the replacement of replaceAll by replace.



but the code is working fine for this




thank's for your replies
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both [ and ] are meta-characters in regular expressions, so both need to be escaped with \\.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code works fine after introducing the escape symbol... thanl's a lot
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic