• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

how to replace the bracket in java?

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 275
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 77261
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the code works fine after introducing the escape symbol... thanl's a lot
 
Campbell Ritchie
Marshal
Posts: 77261
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic