• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

replacing string in java

 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



String str8 = "Hello, John How are you? {{file:350}} with my name";


My requirement is that I want to replace {{file:350}} with some string. number 350 can change. How will i do this? Is their any approach? I was trying to do in this way but it give me exceptions as java.util.regex.PatternSyntaxException: Illegal repetition...

String stro = str8.replaceAll("{*.?}", "00000");
System.out.println("Stro:"+stro);

Does anybody know other way...to do this

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was trying to do in this way but it give me exceptions as java.util.regex.PatternSyntaxException: Illegal repetition...



This is because the curly braces have special meaning in a regex -- it is used to specify the repetition. If you mean to match a curly brace, you need to escape it.

Henry
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this:

String stro = str8.replaceAll("\\{.*\\}", "00000");

{ & } - are special characters.So you have to use \\ like \\{
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you probably want to use ".*?" instead of ".*" in case there can be more than one replacement section in a string.
 
Rahul Ba
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Henry, anathi and Ulf for the kind response.

but before replacing {{file:350}} I need to take out file:350 then need to replace it.




It thwos me exception java.lang.IndexOutOfBoundsException: No group 1. It means
regular expression does work in this kind of secnario any other alaeranatives...
Thanks in advance
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The regexp doesn't use groups, so "matcher.groups(0)" is all you can use; it returns the complete matched substring.
 
Rahul Ba
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String str8 = "Hello, John {{file:350}} how are you?";

Pattern pattern = Pattern.compile("\\{.*?\\}");
Matcher matcher = pattern.matcher(str8);

while(matcher.find()) {
System.out.println(matcher.group(0));
}


This returns {{file:350} I just want file:350
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are going to use groups -- then use them. You need to add groups to the regex before you can call group(1) -- just make sure you configure group one to be the part of the regex that you want.

Since, regex is something that you really should learn -- I'll let you attempt it yourself ... see java.util.regex.Pattern for details.

Henry
 
Rahul Ba
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

I will learn it ...Thanks...
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic