I need to use a replace function in Java:
string.replace(myString,"");
myString values are for example javascript:r(0), javascript:r(23), javascript:l(5) etc. Just number is always different and there is r or l letter. What's the regular expression to match it? Thanks
-
Feel free to do some tests by yourself heretalnicolas– talnicolas2011-08-10 23:04:53 +00:00Commented Aug 10, 2011 at 23:04
-
Did you try anything? At all?f1sh– f1sh2011-08-10 23:19:36 +00:00Commented Aug 10, 2011 at 23:19
-
yea but I don't understand regex expressions very well. I tried some online tools but it didn't work. And also I'm new to Java. Now I have the expression but finding the way how to use it :/simPod– simPod2011-08-10 23:22:44 +00:00Commented Aug 10, 2011 at 23:22
-
try \b(javascript[:][rl][(][0-9][0-9][)]) it matches from from r/l(0) to r/l(99)retornam– retornam2011-08-10 23:27:20 +00:00Commented Aug 10, 2011 at 23:27
Add a comment
|
2 Answers
(FIXED) The regex you want is
"javascript:[rl]\\(\\d+\\)"
NOTE: The outer quotes aren't really part of the regex; they are part of the Java string you pass to Pattern.compile or directly to replace.
Here is a complete program you can try:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class PatternExample {
private static final Pattern JS_PATTERN = Pattern.compile("javascript:[rl]\\(\\d+\\)");
private static final String[] MESSAGES = {
"Good javascript:r(5)",
"Good javascript:l(50003843)",
"Good javascript:r(1123934)",
"Bad javascript:|(5)",
"Bad javascript:r(53d)",
"Bad javascript:l()",
};
public static void main(String[] args) {
for (String s : MESSAGES) {
Matcher matcher = JS_PATTERN.matcher(s);
System.out.println(matcher.replaceAll(""));
}
}
}
Here is another version of the above program that calls replaceAll directly on the string instead of pre-compiling the pattern:
public class PatternExample {
private static final String[] MESSAGES = {
"Good javascript:r(0)",
"Good javascript:l(50003843)",
"Good javascript:r(1123934)",
"Bad javascript:|(5)",
"Bad javascript:r(53d)",
"Bad javascript:l()",
};
public static void main(String[] args) {
for (String s : MESSAGES) {
System.out.println(s.replaceAll("javascript:[rl]\\(\\d+\\)", ""));
}
}
}
8 Comments
simPod
I passed it directly to
replace ( str = str.replace("javascript:[rl]\(\d+\)", ""); ) but it gives me errors (Eclipse) Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )Ray Toal
Right, @simPod, that's what I get for trying to write more than "just the regex" and not testing it. If it's gonna be in a Java string, the backslashes have to be escaped. Very sorry. Edited.
simPod
Ah, thanks. Now there's no error... But it doesn't work... I have this code:
String str = "javascript:r(0)"; str = str.replace("javascript:[rl]\\(\\d+\\)", ""); result is still javascript:r(0). The RegExPlanet says it matches. I don't understand why replace function doesn't work (???)Ray Toal
I added the traditional Pattern/Matcher approach to my answer... let me also add the direct replace approach on your string as well.
Ray Toal
@simPod I added a full program which includes your exact case. Hope you can get it working.
|
The following regex will match it:
javascript:[rl]\(\d+\)
1 Comment
Ray Toal
Whoops, close: the
| inside the square brackets is matched. :)