Java regular expressions using replaceAll?
Clash Royale CLAN TAG#URR8PPP
Java regular expressions using replaceAll?
I have the following problem which states
"Replace all characters in a string with + symbol except instances of the given string in the method"
so for example if the string given was abc123efg and they want me to replace every character except every instance of "123" then it would become +++123+++.
I figured a regular expression is probably the best for this and I came up with this.
str.replaceAll("[^str]","+")
where str is a variable, but its not letting me use the method without putting it in quotations. If I just want to replace the variable string str how can I do that? I ran it with the string manually typed and it worked on the method, but can I just input a variable?
as of right now I believe its looking for the string "str" and not the variable string.
Here is the output its right for so many cases except for two :(
var.replaceAll("[^" + str + "]","+")
when you write [^str] in quotes, it takes the literal characters in that string instead of your variable so it will match to anything that isn't the letter s, t, or r
– faris
14 mins ago
2 Answers
2
This is a bit trickier than you might initially think because you don't just need to match characters, but the absence of specific phrase - a negated character set is not enough. If the string is 123, you would need:
(?<=^|123)(?!123).*?(?=123|$)
https://regex101.com/r/EZWMqM/1/
That is - lookbehind for the start of the string or "123", make sure the current position is not followed by 123, then lazy-repeat any character until lookahead matches "123" or the end of the string. This will match all characters which are not in a "123" substring. Then, you need to replace each character with a +
, after which you can use appendReplacement
and a StringBuffer
to create the result string:
+
appendReplacement
StringBuffer
String inputPhrase = "123";
String inputStr = "abc123efg123123hij";
StringBuffer resultString = new StringBuffer();
Pattern regex = Pattern.compile("(?<=^|" + inputPhrase + ")(?!" + inputPhrase + ").*?(?=" + inputPhrase + "|$)");
Matcher m = regex.matcher(inputStr);
while (m.find())
String replacement = m.group(0).replaceAll(".", "+");
m.appendReplacement(resultString, replacement);
m.appendTail(resultString);
System.out.println(resultString.toString());
Output:
+++123+++123123+++
Note that if the inputPhrase
can contain character with a special meaning in a regular expression, you'll have to escape them first before concatenating into the pattern.
inputPhrase
I updated the post with the result of my old regular expression and you're correct that it does fail in two test cases. I will review your code and try to understand it.
– fsdff
27 mins ago
So instead of coming up with a regular expression that matches the absence of a string. We might as well just match the selected phrase and append +
the number of skipped characters.
+
StringBuilder sb = new StringBuilder();
Matcher m = Pattern.compile(Pattern.quote(str)).matcher(input);
while (m.find())
for (int i = 0; i < m.start(); i++) sb.append('+');
sb.append(str);
int remaining = input.length() - sb.length();
for (int i = 0; i < remaining; i++)
sb.append('+');
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
try
var.replaceAll("[^" + str + "]","+")
– Scary Wombat
56 mins ago