Hello everyone,
i have a problem using the replaceAll method and i hope that somebody can help. I want to remove substrings (sub) from a string (message). The message string looks like that:
prop(ag1,0,1,4,5)/prop(ag2,2,5,3,3)/prop(ag1,0,1,4,5)....
I want to take each prop(...) and put it in a list, but not the duplicate ones.
So i use
String sub = mess.substring(0,mess.indexOf('/'));
and then
mess=mess.replaceAll(sub,"");
but it doesn't work. I printed the mess after the replacement but it's the same and it hasn't removed the substrings. I tried to replace all the "(" just to check
mess=mess.replaceAll("(","r");
but i got that exception
java.util.regex.PatternSyntaxException: Unlcosed group near index 1
(
^.....
...

Does anyone know what's going on?

Dani AI

Generated

As hinted, the root cause is that replaceAll() treats its first argument as a regular expression, so characters like (, ), ., *, +, ?, ^, $, [, ], {, }, | and \ have special meaning. discovered the (/) problem and correctly pointed out . is special. Two practical fixes: treat the substring as a literal pattern, or avoid regex-based replacement entirely.

To treat a substring literally use Pattern.quote(...) so regex meta-characters are escaped automatically. Also remember that the replacement string itself interprets $ and \, so use Matcher.quoteReplacement(...) when the replacement might contain those characters:

mess = mess.replaceAll(java.util.regex.Pattern.quote(sub),
                       java.util.regex.Matcher.quoteReplacement(replacement));

A more robust approach for this specific task (collect distinct prop(...) tokens) is to split or extract tokens and keep unique values rather than repeatedly removing substrings. Splitting and preserving order with a LinkedHashSet is simple and avoids regex pitfalls:

String[] parts = mess.split("/");
Set<String> unique = new LinkedHashSet<String>();
for (String p : parts) if (!p.isEmpty()) unique.add(p);

If you only want properly-formed prop(...) items, extract with a regex that matches the whole token and add matches to a set (this avoids accidental partial matches):

Pattern p = Pattern.compile("prop\\([^)]*\\)");
Matcher m = p.matcher(mess);
Set<String> unique = new LinkedHashSet<String>();
while (m.find()) unique.add(m.group());

Troubleshooting checklist: print sub to confirm its exact contents; prefer Pattern.quote for literal matches; use Matcher.quoteReplacement when replacement contains $ or \; and prefer tokenization or regex extraction for collecting unique tokens so you don't accidentally remove text that only resembles your substring.

Recommended Answers

All 4 Replies

If you simply trying to replace one character like that, use the replace method:

class ReplaceAll
{
  public static void main(String[] args)
  {
	String s = "Replace /, /, and /";
	
	String replace = s.replace('/','r');

	
	System.out.println(replace);
  }
}

A common mistake is for people to expect replaceAll to literally replace all occurrances one string with another, but that's not what it does!
replaceAll() uses a regular expression to determine what to replace. If the string you're giving it as the pattern to replace doesn't compile to what you think it does it will give unexpected results.

Thank you. As i found out the problem is that characters '(' and ')' are treated as special characters by regex so one should use '"\\(" in the left part of the replaceAll in order to work.

i hve seen the above posts... u hve done a good job guys... i like to add that even "." is one of the special characters...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.