File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to replace the bracket in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to replace the bracket in java?" Watch "how to replace the bracket in java?" New topic
Author

how to replace the bracket in java?

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
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

Joined: Jan 08, 2009
Posts: 1398
    
    6



Swastik
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275



Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
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

Joined: May 27, 2011
Posts: 99

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

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
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

Joined: Oct 13, 2005
Posts: 36478
    
  16
Both [ and ] are meta-characters in regular expressions, so both need to be escaped with \\.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
the code works fine after introducing the escape symbol... thanl's a lot
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to replace the bracket in java?
 
Similar Threads
passing java array to javascript
how to display the result with bracket in java?
how to declare array of list for an file
Binary search in sorted array of strings