• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 79928
396
  • 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: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic