1

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

4
  • Feel free to do some tests by yourself here Commented Aug 10, 2011 at 23:04
  • Did you try anything? At all? Commented 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 :/ Commented 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) Commented Aug 10, 2011 at 23:27

2 Answers 2

5

(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+\\)", ""));
        }
    }  
}
Sign up to request clarification or add additional context in comments.

8 Comments

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 \" \' \\ )
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.
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 (???)
I added the traditional Pattern/Matcher approach to my answer... let me also add the direct replace approach on your string as well.
@simPod I added a full program which includes your exact case. Hope you can get it working.
|
5

The following regex will match it:

javascript:[rl]\(\d+\)

1 Comment

Whoops, close: the | inside the square brackets is matched. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.