Regex that matches the string ÷x% [duplicate]

Clash Royale CLAN TAG#URR8PPP
Regex that matches the string ÷x% [duplicate]
This question already has an answer here:
I've been trying to create a regex that matches the following pattern:
÷x%
here is my code:
String string = "÷x%2%x#3$$@";
String myregex = "all the things I've tried";
string = string.replaceAll(myregex,"÷1x#1$%");
I've tried the following regexes: (÷x%) , [÷][x][%] , [÷]1[x]1[%]1
I am using NetBeans IDE and it gives me an
Illegal group reference
However, when I change the value of string to something else, a word for example.
NetBeans does not give me an exception.
any thoughts, thanks
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
When using the
$ in the replacement string for the replaceAll, the engine expects a group number. In your case ` %` is following the $ sign, thus the engine is expecting to find a group named %, which in your case is not something that you have. You will need to escape the $, to something like \$, so that the engine treats it as a literal.– npinti
Aug 6 at 10:16
$
replaceAll
$
%
$
\$
cause this pattern is embedded 1 or more times inside a string. I need to replace all occurrences. @zhh
– i_o
Aug 6 at 10:16
Use
string.replace("+x%", "÷1x#1$%"). Both replace and replaceAll will replace all occurrences. replace doesn't match regex.– zhh
Aug 6 at 10:18
string.replace("+x%", "÷1x#1$%")
replace
replaceAll
replace
@npinti why would it matter to escape the $ for the replacement. Isn't the replacement just a string? I thought the only part that mattered was the regex itself.
– i_o
Aug 6 at 10:20
3 Answers
3
To replace all occurrences of a sub-string you don't need a pattern. You can use String.replace():
String.replace()
String input = "÷x%abc÷x%def÷x%";
String output = input.replace("÷x%", "÷1x#1$%");
System.out.println(output); // ÷1x#1$%abc÷1x#1$%def÷1x#1$%
As per method javadoc:
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
this is something good to know. It simplifies things. thanks
– i_o
Aug 6 at 10:24
As per the comments in the question, I am hoping that this will shed some light on how the replaceAll works.
replaceAll
As per the JavaDoc, the replaceAll takes in a regular expression as first argument. In your case, the regular expression appears to be sound, so there is no issue there.
replaceAll
The second argument that the replaceAll accepts, is the string that will be used to replace whatever the regular expression matches.
replaceAll
In some cases, you will need to replace the same pattern with the same (hard coded, if you will) string:
String myString = "123abc1344";
myString = myString.replaceAll("\d+", "number");
myString = myString.replaceAll("\w+", "word");
System.out.println(myString); //Would yield something of the sort: numberwordnumber
BUT, there are situations were you want use chunks of what you are replacing in the replacement string itself. This is where the $ comes in:
$
String myString = "Age:9;Gender:Male";
Let us say that you want to change the format of the string to the following: "I am a Gender and I am Age years of age.".
In this case, your replacement string needs to extract information from the string to be replaced and inject it in the replacement itself. You do this by using the following:
String myString = "Age:9;Gender:Male";
myString = myString.replaceAll("Age:(\d+);Gender:(\w+)", "I am a $2 and I am $1 years of age.";
The above should yield the string that you are after. Notice that I am using $1 and $2 to access regular expression groups. In regular expression language, the 0th group is whatever it is matched by the entire regular expression. Any other round parenthesis denotes another regular expression group which you can access through the $ keyword.
$
This is why it needs to be escaped.
In the Java Regex you have to escape the $ sign.
If you write $% you would refer to the group % which is not existant.
You can try:
try
String string = "÷x%2%x#3$$@";
String myregex = "÷x%";
String replace = "÷1x#1\$%";
String resultString = string.replaceAll(myregex, replace);
catch (PatternSyntaxException ex)
// Syntax error in the regular expression
catch (IllegalArgumentException ex)
// Syntax error in the replacement text (unescaped $ signs?)
catch (IndexOutOfBoundsException ex)
// Non-existent backreference used the replacement text
Why do you need regex?
– zhh
Aug 6 at 10:14