Reputation: 3031
I am trying to replace java wild character ? from string b.match(/?/g) with \|, using string replaceAll method. I have tried following regex
1. /[\^?]/
2. match[(][/][?][/]g[)]
Both regex work fine with tool but while running java code replaceAll method is not replacing string. Java might be considering ? as regex instead of string. Is there any way to solve the issue.
Upvotes: 1
Views: 434
Reputation: 22992
Both regex work fine with tool but while running java code replaceAll method is not replacing string.
replaceAll
method won't replace your actual String
it will return new String
which has replaced data.
String s="yourString";
s.replaceAll("a","b");//Won't Change your Actual String
String newReplaced=s.replaceAll("a","b")//For Example
Secondly You Need to use Escape Character \
as ?
and |
are special characters and reserved characters for regExp
.
I am trying to replace java wild character ? from string b.match(/?/g) with \|,
Example:
System.out.println("Replace? Marks??".replaceAll("\\?","\\\\|"));
Will replace all ?
with \|
Upvotes: 1