• Post Reply Bookmark Topic Watch Topic
  • New Topic

replacing string in java  RSS feed

 
Rahul Ba
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • 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

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
anandhi mohan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:

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

{ & } - are special characters.So you have to use \\ like \\{
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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: 212
  • Mark post as helpful
  • send pies
  • 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: 42972
73
  • Mark post as helpful
  • send pies
  • 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: 212
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

I will learn it ...Thanks...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!